akumoth/peewee-issues huggingface dataset:
Issue: better documentation of the init function
Author: 42sol-eu
State: closed
Created: 2018-06-07 12:08:02

The initialization of the Database classes with ``None`` as a name is not very obvious.

In my opinion it would be helpful to add this call directly to the quick start example.

see my suggestion at https://github.com/42sol-eu/peewee/blob/master/docs/peewee/quickstart.rst

What would you suggest?


It's documented here: http://docs.peewee-orm.com/en/latest/peewee/database.html#run-time-database-configuration

Is there something specific you feel is missing?

Yes you are right, we had trouble to find it and it is not in any of the tutorial examples. The usage of None on the first parameter is also not described in the parameter description of the classes. This where the reasons for my supossed change in one or more of the quickstart examples. 

>  it is not in any of the tutorial examples

Yes, this is intention as it's not considered the common use-case.

Right at the very top there is a note call-out explaining deferred initialization:

http://docs.peewee-orm.com/en/latest/peewee/api.html#Database

> The database can be instantiated with None as the database name if the database is not known until run-time. In this way you can create a database instance and then configure it elsewhere when the settings are known. This is called deferred initialization.
>
> To initialize a database that has been deferred, use the init() method.
Issue: ManyToManyField doesn't create _through table
Author: kpoman
State: closed
Created: 2018-06-06 23:02:13

Hello, I am trying to implement a multiple choice of a class (Jeweler) for giving optional Style and Specialties choices. I did as follows:




```
import peewee

db = peewee.SqliteDatabase('theone_ai.sqlite', check_same_thread=False)

SPECIALTY_CHOICES = (
    (0, 'Sur mesure'),
    (1, 'Reparations'),
    (2, 'Vente grandes marques'),
    (3, 'Mise au gout bijoux existants'),
    (4, 'Collections propres')
)

STYLE_CHOICES = (
    (0, 'Romantique'),
    (1, 'Contemporait'),
    (2, 'Glam rock'),
    (3, 'Art deco'),
    (4, 'Classique')
)

class BaseModel(peewee.Model):
    class Meta:
        database = db

class Styles(BaseModel):
    style = peewee.IntegerField(choices=STYLE_CHOICES)

class Specialties(BaseModel):
    specialty = peewee.IntegerField(choices=SPECIALTY_CHOICES)

class Jeweler(BaseModel):
    email = peewee.TextField()
    name = peewee.TextField()
    surname = peewee.TextField()
    phone = peewee.TextField()
    raison_sociale = peewee.TextField()
    num_immatriculation = peewee.TextField()
    adresse_siege = peewee.TextField()
    nom_commercial = peewee.TextField()
    styles = peewee.ManyToManyField(Styles, backref='jewelers')
    specialties = peewee.ManyToManyField(Specialties, backref='jewelers')
```





I do create the tables but it doesn't create the _through ones, throwing peewee.OperationalError: no such table: jeweler_styles_through errors.
Is there a way to force peewee to create the auto-through tables ?

Ok ! I found the way:
```
db.create_tables([Jeweler,
                  SalePoint,
                  Styles,
                  Specialties,
                  Jeweler.styles.get_through_model(),
                  Jeweler.specialties.get_through_model()])
```
Now I'm trying to figure out how to populate a peewee object with data from a wtform ...

wtform questions not relevant, but glad you got it sorted.
Issue: Override Model __repr__
Author: albireox
State: closed
Created: 2018-06-06 02:04:07

In peewee 2 I used to be able to override the `__repr__` of a Model by creating a custom class, for example

```python
class MyBase(Model):
    def __repr__(self):
        return '<{0}: {1}>'.format(self.__class__, self.label)
```

This doesn't seem to work anymore in peewee 3 because the `__repr__` is set in `ModelBase.__new__`. Is there a way to override this? I understand that it's still possible to override `__str__`, but I'd like to be able to control the whole string output.

Well, theoretically in your example you would just:

```python
def __str__(self):
     return self.label
```

But for "complete" control...yeah, not do-able at the moment.

What happens when you implement a `__repr__` method on your model class? Are you *also* implementing `__str__()`?

Fixed it and added a test-case showing the behavior you described.

Hi coleifer,

I think it is only half fixed. It now works to define `__repr__` on a model itselt, but not on an intermediate model:
```
#!/usr/bin/env python3

from peewee import *

class MyBase(Model):
    label = CharField()
    def __repr__(self):
        print("__repr__ called")
        return '[{0}: {1}]'.format(self.__class__, self.label)

class MyModel(MyBase):
    pass

class MyModel2(MyModel):
    def __repr__(self):
        print("__repr__ called")
        return '[{0}: {1}]'.format(self.__class__, self.label)

# Does not work
m = MyModel(label = "blah")
print(repr(m))
print(MyBase.__repr__, MyModel.__repr__, m.__repr__, "\n")

# Works
m = MyModel2(label = "blah")
print(repr(m))
print(MyModel2.__repr__, m.__repr__)
```
Output:
```
<MyModel: None>
<function MyBase.__repr__ at 0x7f4656b4b510> <function ModelBase.__new__.<locals>.<lambda> at 0x7f4656ab6378> <bound method ModelBase.__new__.<locals>.<lambda> of <MyModel: None>> 

__repr__ called
[<Model: MyModel2>: blah]
<function MyModel2.__repr__ at 0x7f4656ab6598> __repr__ called
<bound method MyModel2.__repr__ of [<Model: MyModel2>: blah]>
```
The `"__repr__ called"` in the second last line of the output is generated by `print(m.__repr__)` calling `__name__` on the bound function, which itself calls `repr` on the object that `m.__repr__` is bound to (that is `m`) btw.

Peewee is really a nice library :). Very well designed!

@coleifer  is it possible to fix the issue to be able to define `__repr__` on a parent model?
Issue: Upsert documentation is incorrect
Author: bmamouri
State: closed
Created: 2018-06-06 01:00:16

Upsert [documentation](http://docs.peewee-orm.com/en/latest/peewee/querying.html?highlight=upsert#upsert) refers to `on_conflict` method, but that method does not support `conflict_target` parameter anymore.

It is very confusing on what is the right approach to upsert into Postgresql database. I used this and it worked for me:

```
    aid = (Settings
           .update(name='version', value=version)
           .on_conflict()
           .execute())
```

The tests have an example:

```python
    def test_update(self):
        # Conflict on empno - we'll preserve name and update the ID. This will
        # overwrite the previous row and set a new ID.
        res = (Emp
               .insert(first='foo', last='bar', empno='125')
               .on_conflict(
                   conflict_target=(Emp.empno,),
                   preserve=(Emp.first, Emp.last),
                   update={Emp.empno: '125.1'})
               .execute())
        self.assertData([
            ('huey', 'cat', '123'),
            ('zaizee', 'cat', '124'),
            ('foo', 'bar', '125.1')])

        # Conflicts on first/last name. The first name is preserved while the
        # last-name is updated. The new empno is thrown out.
        res = (Emp
               .insert(first='foo', last='bar', empno='126')
               .on_conflict(
                   conflict_target=(Emp.first, Emp.last),
                   preserve=(Emp.first,),
                   update={Emp.last: 'baze'})
               .execute())
        self.assertData([
            ('huey', 'cat', '123'),
            ('zaizee', 'cat', '124'),
            ('foo', 'baze', '125.1')])
```
Issue: BaseModelSelect.get does not accept a db
Author: rammie
State: closed
Created: 2018-06-05 05:30:53

Could `db` be added as an optional parameter to this function that would be passed to `clone.execute()`? -
https://github.com/coleifer/peewee/blob/master/peewee.py#L5724

This would allow us to have much cleaner code when fetching a single row from a replica. As always, thanks so much for peewee!

When dealing with anything `Model`-related, the convention throughout the code is to rely on the database in `Model._meta.database`. The lower-level query-builder APIs support an explicit database parameter, however. For consistency it would also probably needed to be added everywhere else a model query can be executed (implicitly or explicitly), which I am definitely not interested in doing.

For what it's worth, you can use `database.bind_ctx`:

http://docs.peewee-orm.com/en/latest/peewee/api.html#Database.bind_ctx

Or from the model:

http://docs.peewee-orm.com/en/latest/peewee/api.html#Model.bind_ctx

Just for clarification, would you recommend against using code like `query.execute(replica_db)`?

It's just that we found that pattern very convenient when we migrated to using read replicas. We were able to leave the query generation unchanged and decide which database to hit later. The `bind_ctx` decorator is nice, but it requires us to know which models are used by the query at the time of choosing the database. Though this can be tackled by inspecting the query, that seems a bit undesirable. 

Amongst the methods supported by `ModelSelect`, the following all support a database parameter -  `count`, `scalar`, `iterator`, `execute`, `dicts`, `tuples`, `peek`, `first`, `exists`. The `get` method, which is inherited from `BaseModelSelect.get` instead of `Select` is the only one I could find that does not support a database parameter. I understand the argument for consistency across the board, but it would be nice for it to be consistent amongst the methods exposed by `ModelSelect`. Unless of course you recommend against using `execute(db)` directly at all.

Is there any chance you would you reconsider this change? please :). If you're ok with it I can put together a PR.

Standard note of appreciation: Peewee is the best ORM I've ever used. Thank you so much for supporting it for so long!

Interesting...let me give this a think when I get back to my home in the next couple days.

Implemented for `ModelSelect.get()`, thanks for doing the work of looking into the existing APIs and showing where `database` is accepted. Appreciate it!

Thank you so much! Really appreciate this.
Issue: importing peewee hangs debugging on windows
Author: GitHubAre
State: closed
Created: 2018-06-02 17:40:13

Hello,
I am using Visual Studio Code and Windows 10 for development
I have installed peewee using pip.
When having _from peewee import *_ I cant debug and everything hangs on the import line.
If I remove breakpoints from Visual Studio Code it does not hang.
Can you please suggest what could be problem? What is happening behind the scenes that could block the debugger?


Try to use Pycharm. 

pycharm works fine, thank you!
Issue: use value as LIKE left hand operand
Author: gbour
State: closed
Created: 2018-05-29 07:49:38

Hello,

In sqlite, we can do a query like that (prefix being a field of mytable table):
`SELECT * FROM mytable WHERE 123456 LIKE prefix||'%'`

same in mysql:
`SELECT * FROM mytable WHERE 123456 LIKE concat(prefix,'%')`

It will match prefixes like '12' or '1234'. I use it for prefix matching.
Unfortunately with peewee, *%* left hand must be the table field.

Is there a proper way to do that ?
Thanks for your help.


Sure, you can use `Value(123456)`:

```python

# If prefix is a string literal:
query = SomeTable.select().where(Value('123456').startswith('someprefix'))

# If prefix is a column on the table:
query = SomeTable.select().where(Value('123456').startswith(SomeTable.prefix))
```

Oh really good, I did not found it in the documentation
Thanks very much @coleifer, you made my day :+1:  

this is only in 3.4.0 version, right ?

The `Value` type was added in 3.0. Prior to 3.x you may be able to use `Param` instead.
Issue: Problem serializing query result  to JSON
Author: mgraupner
State: closed
Created: 2018-05-25 08:48:25

Following model:

```
class User(BaseModel):
    ...

class Event(BaseModel):
    ...
    user = ForeignKeyField(User, related_name='events')
```

I want to have the newest event for each user:
```
        result = (User
                 .select(User, Event)
                 .join(Event)
                 .group_by(Event.user_id)
                 .having(fn.MAX(Event.create_date))
                 .order_by(Event.create_date.desc())
                 )
```

and then serialize the result to JSON:
```
serialized = [model_to_dict(r, recurse=True, backrefs=True) for r in result]
```

unfortunately now ALL events are included in the serialized data (guess that's what backrefs does), not just the newest like in my query. Is this the intended behaviour? How can I only get the user/event I was selecting for in the query?

Thanks for your help.

Yes, this is the intended behavior. That group by looks sketchy as hell since you're selecting columns that aren't included in the group by...granted, that probably works fine for SQLite and MySQL. Anyways!

`model_to_dict` probably isn't going to work correctly for what you're trying to do without some gymnastics. It's probably much easier to just do something like:

```python
accum = []
for user_obj in result:
    user_dict = model_to_dict(user_obj, recurse=False)
    user_dict['latest_event'] = model_to_dict(user_obj.event, recurse=False)
```

Thanks, the group_by clause had the wrong content (fixed now).

Does your solution still apply or is there some automated way now? Or is there a better way for the query so that there is an automated way?

The solution I gave is probably the easiest way to achieve what you want. Events is a backref from the perspective of the user object, so peewee treats it as a list. Since you only need the one event per user, just use the approach I suggested.

The given example works as expected. Thank you very much for your help. I think I just expected too much automatism after reading the manual. :)

Some minor stuff: I think you forgot to add the user_dict to the accum list in your example.
Issue: SchemaMigrator.add_column ignores the index_type, always results a btree index.
Author: wonderbeyond
State: closed
Created: 2018-05-25 06:26:17

with `db.create_tables`, I can get an `GIN` index for a field like below:

```python
BinaryJSONField(default=dict, index=True, null=True)
```

However, if I add a new column with `migrator.add_column`, I always get a btree index.

You may need inspecting code here: https://github.com/coleifer/peewee/blob/master/playhouse/migrate.py#L290

Thanks, I've fixed this in 8a95426 and added a test and updated docs. I appreciate you taking the time to report this issue.
Issue: table_exists() function not working.
Author: djh2020
State: closed
Created: 2018-05-25 03:15:45

I am unable to get this function to work.  I have tried (table='job'),('job'),(job),(['job']) 

![screen shot 2018-05-24 at 4 55 31 pm](https://user-images.githubusercontent.com/6467508/40524375-154da6fa-5f8f-11e8-96c5-0a1644a695fa.png)


So in examining / digging through your tests I see your documentation isn't accurate.  Looks like it needs to be updated.

https://github.com/coleifer/peewee/blob/49c162d5939bb52627c1e6a1687e722b8bf83ec9/tests/database.py#L398


documented
<img width="702" alt="screen shot 2018-05-24 at 8 22 18 pm" src="https://user-images.githubusercontent.com/6467508/40524715-02f652c0-5f91-11e8-9f98-fa47bc38ebec.png">


actually...
<img width="341" alt="screen shot 2018-05-24 at 8 24 11 pm" src="https://user-images.githubusercontent.com/6467508/40524726-16597fe0-5f91-11e8-85a6-a36e768d2cc3.png">


Thank you, I've fixed this so that the implementation matches the docs (i.e., you pass the table name).
Issue: get_or_create violates unique constraint when saving
Author: albireox
State: closed
Created: 2018-05-21 21:33:45

If I do

```python
dbo, created = Plate.get_or_create(plate_id=1234)
if created is True:
    dbo.chunk = 'a'
    dbo.save()
```

O get an error

```python
IntegrityError: duplicate key value violates unique constraint "plate_id_uniq"
DETAIL:  Key (plate_id)=(1234) already exists
```

This seems to happen because the `.save()` method tries to create the record again, instead of updating it. This code used to work with peewee 2.


Can you create a minimal test-case I can use to reproduce the issue? I'm unclear from your comment what your model looks like or where the actual error is occurring.

I don't seem to be able to reproduce this on a simple case so it may have something to do with my DB (Postgresql). For what I can see, the problem is related to the fact that, although the primary key in Plate has a sequence set, when I do the 

```python
dbo, created = Plate.get_or_create(plate_id=1234)
```

The resulting object doesnt have a pk

```python
dbo
<Plate: pk=None, plate_id=1234>
```

Other tables in the same DB, with primary key sequences set in the same manner return an object with a `pk != None` when initialised with `get_or_create`. I'll keep digging.



Ok, I think I fixed it. In my Models some of the primary keys were set to `BigIntegerField` instead of to `PrimaryKeyField` because the underlying primary key field was set as a big integer in Postgresql. Changing it to `PrimaryKeyField` seems to have solved the issue and I don't see any ill effect.

Worth noting that the models were generated using pywiz, although the peewee 2 version of it. Also, this used to work fine in peewee 2 so not sure if this is a bug or not.

Sounds like you got it.

For what it's worth, in peewee 3.x, you can use `AutoField` or `BigAutoField` depending on the underlying data-type (serial vs bigserial). `PrimaryKeyField` is provided for backwards-compatibility, but is just an alias for `AutoField`.
Issue: Can't add parameters for DecimalField used in ArrayField (peewee 2.10.2)
Author: ssrebelious
State: closed
Created: 2018-05-19 18:09:24

I have a model  where I need to define custom parameters for DecimalField that is used inside ArrayField:
```python
class Pricing(Model):
    base_cost = ArrayField(
        DecimalField,
        max_digits=16,
        decimal_places=2,
        help_text='Base cost',
        null=False
    )
    class Meta:
        database = postgres_db
```
This results in error telling me that `max_digits` and `decimal_places` are not supported parameters for `ArrayField`. I took a quick peek into `ArrayField` class and I guess that the `__init__` method might be an issue: *args and **kwargs are passed as parameters to `self.__field = field_class(*args, **kwargs)` and `super(ArrayField, self).__init__(*args, **kwargs)`.

As a workaround I could subclass `DecimalFiled` with required parameters and pass it to `ArrayField`. Is there another way?

That's a nice workaround but I'll see what I can do to offer better support.

This fix will be in the 3.4.0 release. If you plan to continue using 2.10 then the subclassing solution is totally fine.
Issue: [Feature Request] iregex support
Author: wonderbeyond
State: closed
Created: 2018-05-18 10:07:54

See https://docs.djangoproject.com/zh-hans/2.0/ref/models/querysets/#iregex

Peewee supports a regexp operator. Depending on the database you're using, you can utilize the db-specific features to handle case-sensitivity.

Added support for MySQL and Postgresql. SQLite is going to be trickier and I may not implement it.

See: f631f222fe6b0c5cb68de99dede98d03454d3e3f
Issue: Get untouched database field values
Author: timlehr
State: closed
Created: 2018-05-14 21:17:06

Hi Charles,

in Peewee 2 I was using the `prepared()` function to store the initial field values selected from the DB in a dictionary, so I could compare it with `__data__` at any time to determine if changes have been made. I know that the dirty field is supposed to offer this functionality, but using the dirty flags, I can not determine if the user has undone his changes. Since `prepared()` is gone in version 3, I'm struggling to find a function for my subclasses to do the same thing again. Any suggestions on this matter?

Best,
Tim

That is the purpose of `dirty_fields`. For your use-case I'd suggest putting an interface on-top of your model instance and routing accesses through that. In that way you can track changes.
Issue: why not model.DoesNotExist inherite form Exception?
Author: xvdy
State: closed
Created: 2018-05-11 07:12:06

```
    @asyncio.coroutine
    def get(self, source_, *args, **kwargs):
        """Get the model instance.

        :param source_: model or base query for lookup

        Example::

            async def my_async_func():
                obj1 = await objects.get(MyModel, id=1)
                obj2 = await objects.get(MyModel, MyModel.id == 1)
                obj3 = await objects.get(MyModel.select().where(MyModel.id == 1))

        All will return `MyModel` instance with `id = 1`
        """
        yield from self.connect()

        if isinstance(source_, peewee.Query):
            query = source_
            model = query.model_class
        else:
            query = source_.select()
            model = source_

        conditions = list(args) + [(getattr(model, k) == v)
            for k, v in kwargs.items()]

        if conditions:
            query = query.where(*conditions)

        try:
            result = yield from self.execute(query)
            return list(result)[0]
        except IndexError:
            print(type(model.DoesNotExist))
            print(type(Exception))
            print(isinstance(model.DoesNotExist, Exception))
            raise model.DoesNotExist
```

print(isinstance(model.DoesNotExist, Exception)) # False

`DoesNotExist` is not an instance but a class:

```python
isinstance(Model.DoesNotExist, Exception) -- False
issubclass(Model.DoesNotExist, Exception) -- True
```

You catch it like this:

```python
try:
    obj = SomeModel.get(SomeModel.field == 'value')
except SomeModel.DoesNotExist:
    print('obj with field=value does not exist')
```