akumoth/peewee-issues huggingface dataset:
Issue: choices are not working!
Author: surajano
State: closed
Created: 2018-10-30 08:26:02

In the following code the choices for Task tables are not working, while creating the instance of Task with status="Success",  its not adding the choices value(ie "S") in the task DB table. Its adding the "Success" as it is in the task table.
```
from datetime import datetime
import peewee

db = peewee.SqliteDatabase('peewee_choices.db')


class Task(peewee.Model):
    """
    Some Task table Model
    """
    TASK_STATUS = (
        ("S", "Success"),
        ("F", "Failure"),
        ("U", "Unknown"),
    )

    status = peewee.CharField(choices=TASK_STATUS)
    timestamp = peewee.DateTimeField(default=datetime.utcnow())

    class Meta:
        database = db


if __name__ == "__main__":
    db.connect()
    db.create_tables([Task])
    task_events_1 = Task(status="Success")
    task_events_1.save()
    task_1 = Task.get(status="Success")
    print("Task 1 Status : ", task_1.status) # Task 1 Status :  Success

    task_events_2 = Task(status="S")
    task_events_2.save()
    task_2 = Task.get(status="S")
    print("Task 2 Status : ", task_2.status) # Task 2 Status :  S
    db.drop_table(Task)
    db.close()


```
When we run the above code it will print the following:
Task 1 Status :  Success
Task 2 Status :  S


* http://docs.peewee-orm.com/en/latest/peewee/api.html?highlight=choices#Field
* http://docs.peewee-orm.com/en/latest/peewee/models.html?highlight=choices#some-fields-take-special-parameters

> Both default and choices could be implemented at the database level as DEFAULT and CHECK CONSTRAINT respectively, but any application change would require a schema change. Because of this, default is implemented purely in python and **choices are not validated but exist for metadata purposes only.**

If you want to enforce a particular value that should be done using a CHECK constraint:

```python
status = CharField(constraints=[Check("status IN ('S', 'F', 'U')")])
```
Issue: prefetch not work when joining same model
Author: 1xinghuan
State: closed
Created: 2018-10-22 14:56:13

I have some models like this:

```
class Version(BaseModel):
    name = CharField()

    @property
    def sources(self):
        return [i.from_version for i in self.sources_connection]

    @property
    def destinations(self):
        return [i.to_version for i in self.destinations_connection]


class VersionRelationship(BaseModel):
    from_version = ForeignKeyField(Version, backref='destinations_connection')
    to_version = ForeignKeyField(Version, backref='sources_connection')

query = Version.select().where(Version.id > 12700)
# query = query.limit(20)

subquery1 = VersionRelationship.select(VersionRelationship, Version).join(Version, on=VersionRelationship.from_version)
subquery2 = VersionRelationship.select(VersionRelationship, Version).join(Version, on=VersionRelationship.to_version)


prefetch(query, subquery1)
prefetch(query, subquery2)


for q in query:
    print q.sources_connection
    print q.destinations_connection

    print q.sources
    print q.destinations
```

When I only prefetch subquery1 and only view sources, it works fine. But doesn't work if I prefetch both and view sources and destinations.
Did I miss something or it's a bug?
Issue: prefetch doesn't work when join same model
Author: 1xinghuan
State: closed
Created: 2018-10-22 14:30:00

I have some models like this:

```
class Version(BaseModel):
    name = CharField()

    @property
    def sources(self):
        return [i.from_version for i in self.sources_connection]

    @property
    def destinations(self):
        return [i.to_version for i in self.destinations_connection]


class VersionRelationship(BaseModel):
    from_version = ForeignKeyField(Version, backref='destinations_connection')
    to_version = ForeignKeyField(Version, backref='sources_connection')

query = Version.select().where(Version.id > 12700)
# query = query.limit(20)

subquery1 = VersionRelationship.select(VersionRelationship, Version).join(Version, on=VersionRelationship.from_version)
subquery2 = VersionRelationship.select(VersionRelationship, Version).join(Version, on=VersionRelationship.to_version)


prefetch(query, subquery1)
prefetch(query, subquery2)


for q in query:
    print q.sources_connection
    print q.destinations_connection

    print q.sources
    print q.destinations
```

When I only prefetch subquery1 and only view sources, it works fine. But doesn't work if I prefetch both and view sources and destinations.
Did I miss something or it's a bug?



wrong submit.
Issue: prefetch not work when join to same model
Author: 1xinghuan
State: closed
Created: 2018-10-22 14:26:36

I have some models like this:

```
class Version(BaseModel):
    name = CharField()

    @property
    def sources(self):
        return [i.from_version for i in self.sources_connection]

    @property
    def destinations(self):
        return [i.to_version for i in self.destinations_connection]

class VersionRelationship(BaseModel):
    from_version = ForeignKeyField(Version, backref='destinations_connection')
    to_version = ForeignKeyField(Version, backref='sources_connection')

query = Version.select().where(Version.id > 12700)
# query = query.limit(20)

subquery1 = VersionRelationship.select(VersionRelationship, Version).join(Version, on=VersionRelationship.from_version)
subquery2 = VersionRelationship.select(VersionRelationship, Version).join(Version, on=VersionRelationship.to_version)

prefetch(query, subquery1)
prefetch(query, subquery2)

for q in query:
    print q.sources_connection
    print q.destinations_connection
    
    print q.sources
    print q.destinations
```

When I only prefetch subquery1 and only view sources, it works fine. But doesn't work when prefetch subquery2 and view destinations.

Did I miss something or it's a bug?
Issue: query result to python list
Author: michalchrzastek
State: closed
Created: 2018-10-20 09:47:03

Hello,

I'm trying to return  a list with integers (ID column) like so:

`my list = [1,2,3,4,...]`

 but get this error, what am I missing?

`TypeError: <modelFinance.tag_list object at 0x759703d0> is not JSON serializable`

my query:

`def get_tagsID(self):
		tagQ = tag_list.select(tag_list.id)
		return list(tagQ)`


It's returning a list of tag instances...you need to:

```python
def get_tagsID(self):
    tagQ = tag_list.select(tag_list.id)
    return [tl.id for tl in tagQ]
```

Also python typically prefers lower case w/underscores instead of that camelCase just fyi.

By the way, try reading the error message...it seems completely obvious to me at least.

Thank you, it works perfectly. No sorry, don't understand the error...
Issue: Extending JSONField to rename-able keys
Author: patarapolw
State: closed
Created: 2018-10-19 02:27:03

I am thinking of doing something like this:

    class JSONValueField(JSONField):
        def __init__(json_key, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.ref = json_key
        
        def db_value(self, value):
            d = dict()
            for k, v in value.items():
                d[self.ref.from_(k)] = v

            return json.dumps(d, ensure_ascii=False)
        
        def python_value(self, value):
            d = dict()
            for k, v in json.loads(value).items():
                d[self.ref.to_(k)] = v
            
            return d
    
    
    class JSONKeyField(JSONField):
        def __init__(*args, **kwargs):
            super().__init__(*args, **kwargs)
            self.from_ref = dict()
        
        def python_value(self, value):
            d = json.loads(value)
            for k, v in d.items():
                self.from_ref[v] = k
            
            return d
        
        def to_(self, k):
            return self[k]
        
        def from_(self, v):
            return self.from_ref[v]

        # For `self.db_value`, probably fill with k, uuid4(), like
        def add(self, k):
            self[k] = uuid4()

        def remove(self, k):
            self.pop(k)

Or Probably, extending ManyToManyField()

BTW, is there an easy way to rename Key in JSON, for every entries?

I don't understand the code.

For sqlite json, I imagine it is possible but not sure off the top of my head.
Issue: model_to_dict + ManyToManyField shows abthrough_set instead of bs
Author: patarapolw
State: closed
Created: 2018-10-18 12:10:50

    >>> model_to_dict(db['card'].select()[1], backrefs=True)
    {'id': 1520716970993,
     'note': {'id': 1520716965129,
      'data': {'中文': '我们现在没有钱。',
       'Pinyin': 'Wǒmen xiànzài méiyǒu qián.',
       'English': "We don't have money now.",
       'Notes': '',
       'Example Type': '',
       'Article Title': 'Negation of "you" with "mei"',
       'Grammar Construct': '没 + 有 (+ Obj.)',
       'Source URL': 'https://resources.allsetlearning.com/chinese/grammar/ASGPNV3Q'},
      'notetagthrough_set': [{'id': 7, 'tag': {'id': 2, 'name': 'Adverbs'}},
       {'id': 6, 'tag': {'id': 3, 'name': 'Parts_of_Speech'}},
       {'id': 8, 'tag': {'id': 4, 'name': 'A1'}}]},
     'deck': {'id': 1496896533573, 'name': 'Chinese Grammar Wiki'},
     'template': {'id': 2,
      'model': {'id': 1496733135171,
       'name': 'Chinese Grammar Wiki',
       'css': '.card {\nfont-family: arial;\nfont-size: 30px;\ntext-align: center;\ncolor: black;\nbackground-color: white;\n}\n\n.hanzi {\nfont-family: SimSun;\nfont-size: 28px;\n}\n\n.pinyin {\ncolor: gray;\n}\n\n.translation {\nfont-size: 26px;\n}\n\n.lessonInfoHanzi {\nfont-family: SimSun;\nfont-size: 12px;\n}\n\n.lessonInfo {\nfont-family: arial;\nfont-size: 10px;\n}\n\n.english {\nfont-family: arial;\nfont-size: 20px;\n}\n\n.notes {\nfont-family: arial;\nfont-size: 12px;\ncolor: gray;\n}\n\n.spacer {\nheight: 20px;\n}\n\n.spacerSmall {\nheight: 3px;\n}\n\n.spacerMedium {\nheight: 10px;\n}\n'},
      'name': 'Translation',
      'question': '<div class="hanzi">{{中文}}</div>',
      'answer': '{{FrontSide}}\n<div class="spacer"> </div>\n<div class="english">\n\t<div class="pinyin">{{Pinyin}}</div>\n\t<div class="spacerSmall"> </div>\n\t{{English}}\n\t<div class="spacerMedium"> </div>\n\t<div class="notes">{{Notes}}</div>\n</div>\n\n\n<div class="lessonInfo">\n<div class="spacer"> </div>\n<div>Pattern: <span class="lessonInfoHanzi">{{Grammar Construct}}</span></div>\nArticle: <a href={{Source URL}}>{{Article Title}}</a>\n</div>'},
     'srs_level': None,
     'next_review': None}

It should be `tags`, not `notetagthrough_set`.

You should use `manytomany=True`:

http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#model_to_dict
Issue: .delete_instance() does not delete ManyToManyField's relationship, causing duplicate key on next create
Author: patarapolw
State: closed
Created: 2018-10-17 11:42:24

    class Tag(BaseModel):
        name = TextField()

    class Data(BaseModel):
        tags = ManyToManyField(Tag, backref='data')

    DataTag = Data.tags.get_through_model()

    db_data = Data.create()
    Tag.get_or_create(name='foo')[0].data.add(db_data)
    Tag.get_or_create(name='bar')[0].data.add(db_data)
    db_data.delete_instance()
    db_data = Data.create()
    Tag.get_or_create(name='foo')[0].data.add(db_data)

In this case, it will raise an error. Also, the newly created `db_data` persists in the database having tags `['foo', 'bar']`.

You have a couple options.

You can specify `on_delete='cascade'`, which will automatically delete the DataTag objects when a Data or Tag gets deleted:

```python
tags = ManyToManyField(Tag, backref='data', on_delete='cascade')
```

Or, you can specify ``recursive=True`` when calling `delete_instance()`.

```python
db_data.delete_instance(recursive=True)
```
Issue: A curious key represent and a little prob
Author: piperck
State: closed
Created: 2018-10-17 04:04:51

```python
In [1]: from apps.models.offline.user import *

In [2]: from peewee import *

In [16]: User.select(fn.MAX(User.id), fn.MIN(User.id)).sql()
Out[16]: ('SELECT MAX(`t1`.`id`), MIN(`t1`.`id`) FROM `user` AS `t1`', [])

In [17]: User.select(fn.MAX(User.id), fn.MIN(User.id)).__dict__
Out[17]:
{'_alias': None,
 '_constructor': None,
 '_cte_list': None,
 '_cursor_wrapper': None,
 '_database': <db.init_db.MyRetryDB at 0x5164810>,
 '_distinct': None,
 '_for_update': None,
 '_from_list': [<Model: User>],
 '_group_by': None,
 '_hash': 7290913799531828381,
 '_having': None,
 '_is_default': False,
 '_join_ctx': <Model: User>,
 '_joins': {},
 '_limit': None,
 '_offset': None,
 '_order_by': None,
 '_returning': [<peewee.Function at 0x540c450>,
  <peewee.Function at 0x540c410>],
 '_row_type': None,
 '_simple_distinct': None,
 '_where': None,
 '_windows': None,
 'model': <Model: User>}

In [18]: User.select(fn.MAX(User.id), fn.MIN(User.id))[0].__dict__
Out[18]: {'__data__': {}, '__rel__': {}, '_dirty': set(), '`id`)': 2493977}

In [19]: User.select(fn.MAX(User.id), fn.MIN(User.id))[0]
Out[19]: <User: None>

In [20]: User.select(fn.MAX(User.id), fn.MIN(User.id))[0].__dict__
Out[20]: {'__data__': {}, '__rel__': {}, '_dirty': set(), '`id`)': 2493979}

In [21]: User.select(fn.MAX(User.id), fn.MIN(User.id))[0].__dict__['`id`)']
Out[21]: 2493982

In [22]: User.select(fn.MIN(User.id), fn.MAX(User.id))[0].__dict__
Out[22]: {'__data__': {}, '__rel__': {}, '_dirty': set(), '`id`)': 1}


In [24]: User.select(fn.MIN(User.id), fn.MAX(User.uid))[0].__dict__
Out[24]:
{'__data__': {},
 '__rel__': {},
 '_dirty': set(),
 '`id`)': 1,
 '`uid`)': u'fffff9fa480038b7e589b7890418b1e3'}
```

Hi:
I have two problem with these code.
First one When I use `User.select(fn.MAX(User.id), fn.MIN(User.id))[0]` why I just get one value `fn.MAX(User.id)` but can not get the `MIN(User.id)` value?

Second one.... when I use .__dict__ to find out why. I have in confused with key `\`id`)\` . Is this deliberate？
Anyone can help me to resolve these problem?


For scalar aggregations, you can:

```
max_id, min_id = User.select(fn.MAX(User.id), fn.MIN(User.id)).scalar(as_tuple=True)
```

If you want "User" instances, then assign an alias to disambiguate the selected columns:

```
query = User.select(fn.MAX(User.id).alias('max_id'), fn.MIN(User.id).alias('min_id'))
user_obj = query[0]
print(user_obj.max_id, user_obj.min_id)
```
Issue: peewee.OperationalError: SSL error: bad decrypt
Author: maxcountryman
State: closed
Created: 2018-10-16 17:54:32

Running into this issue when spawning processes with `concurrent.futures.ProcessPoolExecutor`.

I'm guessing you're opening the database connection before the fork? Try opening the connection after forking. 

Actually no, I’m not. The connection is initialized in the function the executor handles. 

Can you share a minimal example?

If you could please let me know:

* What database engine / python driver combination are you using?
* What is the output of "print(db.is_closed())" when your forked processes are starting up?
* Are you sure it's not possible that some connection state is copied to the child process when forking?
Issue: fix bug group by extend
Author: nhatHero
State: closed
Created: 2018-10-12 05:58:27

I fixed bug group by extend
- Examples:
users table have columns: id and username
Tweet table have contain user_id column. user_id is foreign key of users table

```sql
   query = (User
                 .select(User, fn.COUNT(Tweet.id).alias('tweet_count'))
                 .join(Tweet, JOIN.LEFT_OUTER)
                 .group_by_extend(User.id).group_by_extend(User.username))
```
 
Then I run this query.

- Before I fix bug then result of query :
```sql
SELECT t1.id, t1.username, count(t2.id) as tweet_count
FROM users as t1
LEFT OUTER JOIN tweet as t2 on (t2.user_id = t1.id)
```

-  After I fix bug then result of query:
```sql
SELECT t1.id, t1.username, count(t2.id) as tweet_count
FROM users as t1
LEFT OUTER JOIN tweet as t2 on (t2.user_id = t1.id)
GROUP BY t1.id, t1.username  # I fixed to add group by
```

Thank you!
Issue: elastic-apm intrumentation
Author: claygorman
State: closed
Created: 2018-10-11 19:58:21

Hey there,

Great library by the way! I use it quite a bit.

I was wondering what your thoughts are on integrating with https://github.com/elastic/apm-agent-python/blob/master/elasticapm/instrumentation/register.py as a built in?

Thanks in advance!

I'm glad you like the library.

Sorry, never heard of this project.
Issue: 1. Cannot create unique index on large text fields 2. Documentation regarding PostGresQL full-text search is unclear
Author: patarapolw
State: closed
Created: 2018-10-09 05:24:10

What is `query` in `blog_search(query)` in http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#full-text-search ?

As a matter of fact, I am trying to do:

    class Cell(BaseModel):
        content = pv.TextField(unique=True)
    
    db_cell = Cell.get_or_none(Cell.content.contains(new_data))
    if db_cell is None:
        db_cell = Cell.create(
            content=new_data
        )

My current solution:

    class Cell(BaseModel):
        content = pv.TextField()
        search_content = TSVectorField()
        
        @classmethod
        def add_content(cls, content):
            return cls.create(
                content=content,
                search_content=fn.to_tsvector(content)
            )
        
    db_cell = Cell.get_or_none(Cell.search_content == fn.to_tsvector(new_data))

    if db_cell is None:
        db_cell = Cell.add_content(
            content=new_data
        )

I can't seem to add `unique=True` to content.

What are you asking? The title of the issue says one thing, but the question within your post seems to be about adding a `unique` constraint to a `TextField`?

In the example, "query" is a text search query. I'll rename to make it more clear.

aa12e607e1b8d073875afbc761c55a2b572271aa

> In the example, "query" is a text search query. I'll rename to make it more clear.

'cat & rat' as in `to_tsquery('cat & rat')`? https://www.postgresql.org/docs/9.1/static/functions-textsearch.html

> What are you asking?

Sorry. I did ask two questions. The first question is well answered, but for the second question, I suppose the answer is 

    content = TextField(unique=False)

Then,

    db.execute_sql('''
    CREATE UNIQUE INDEX unique_content_index ON cell (md5(content));
    ''')

But, how do I do it in pure peewee?

You don't need to specify `unique=False` - this is the default behavior.

To create an index on an expression like that you have a couple options:

```python

# Method one.
Cell.add_index(fn.MD5(Cell.content), name='cell_content_hash', unique=True)

# Method two.
idx = Cell.index(fn.MD5(Cell.content), name='cell_content_hash', unique=True)
Cell.add_index(idx)

# Method three.
class Cell(Model):
    ...
    class Meta:
        indexes = [
            SQL('create unique index cell_content_hash on cell (md5(content))'),
        ]
```

Thanks for the answer. I would choose Method three.

Although, it would be simpler if I can do something directly in `content = TextField(index=...)` like

    content = TextField(index=fn.MD5, unique=True)

As a matter of fact, my problem was originally the content is so large the PostGres cannot unique-ify it.
Issue: Edit field type with Migrator
Author: bastiendonjon
State: closed
Created: 2018-10-08 10:02:06

Hello,

It's not possible to change field type with migrator ?

I want edit DateField to DateTimeField

Thanks

Using peewee's migrating tools you'd need to drop the field and recreate it. Performing column changes requires (sometimes) changing any data present within the column, and that's left to the application rather than being handled by the library.
Issue: DeprecationWarning Using or importing the ABCs from 'collections'
Author: trim21
State: closed
Created: 2018-10-07 13:01:22

peewee.py:2256: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working
  is_dict = isinstance(row, collections.Mapping)

python version 3.7.0
peewee version 3.7.1

49ca301f319a6a70d7acd1425b66fa5cbdf75d8e
Issue: TimestampField appears to strip milliseconds
Author: tbaxter-18f
State: closed
Created: 2018-10-04 19:31:40

given a timestamp like 1419440770.000058 when I create the object, the timestamp appears to be stored as 1419440770, with no milliseconds present any longer.

Well, here's the thing. Peewee's `TimestampField` is designed to work with python `datetime` objects, which is typically what you want in your application code -- it just uses an integer timestamp for storage. That being said, you still *can* specify integer / floating-point timestamps. For sub-second resolutions you will just need to also specify the desired resolution when instantiating the field, expressed as a power-of-10. This works because behind-the-scenes Peewee just multiplies the timestamp by a power-of-10 to obtain an integer.

```python
class Event(Model):
    timestamp = TimestampField(resolution=1e6)  # microseconds.
    data = TextField()

# We can specify an integer or floating-point timestamp -- peewee will do the right thing:
ts = 1500000000.123456  # 2017-07-13 21:40:00.123456
Event.create(timestamp=ts, data='event-1')

# Or we can just use a datetime object:
dt = datetime.datetime(2018, 1, 2, 3, 4, 5, 6789)
Event.create(timestamp=dt, data='event-2')

# Peewee converts to datetimes when reading from the db:
for event in Event.select():
    print(event.data, ':', event.timestamp)

# Prints:
# event-1 : 2017-07-13 21:40:00.123456
# event-2 : 2018-01-02 03:04:05.006789
```