akumoth/peewee-issues huggingface dataset:
Issue: Question. Migrations maker
Author: mrkovalchuk
State: closed
Created: 2020-05-15 12:46:07

Peewee it's enough popular python ORM library. Have the migrations system. But he hasn't instrument for auto make migrations. Like in django, or Alimbic for SQLAlchemy.

Is any work in this direction? https://pypi.org/project/peewee-migrations/ - like this (but more life full)

Peewee has a very easy-to-use migrations helper that you can use to modify your schema. At the time of writing, an automatic "detect changes and generate migrations" tool is not part of Peewee and likely will not be added at any time. Such tools carry a very large maintenance burden and are buggy, and bugs can have disastrous effects on your db if you aren't careful. I prefer explicit migrations, and Peewee supports that.

You're welcome to use a 3rd party tool, but I can't vouch for any of them, not having used them myself.
Issue: Database.session_start() locking error
Author: hfaua
State: closed
Created: 2020-05-14 19:55:55

Problem:
When `Database.session_start()` is called before opening the connection to the database it tries to acquire the lock recursively first time in the `Database.session_start()` and second time in `Database.begin()`.

```
peewee.py:3195
def session_start(self):
    with self._lock:
->      return self.transaction().__enter__()
```
When entering `self.transaction().__enter__()` the lock is already acquired. Then it goes to:
```
peewee.py:4148
def __enter__(self):
    if self.db.transaction_depth() == 0:
->      self._begin()
    self.db.push_transaction(self)
    return self
```
```
peewee.py:4134
def _begin(self):
    args, kwargs = self._begin_args
->  self.db.begin(*args, **kwargs)
```
```
peewee.py:3245
def begin(self):
    if self.is_closed():
->      self.connect()
```
```
peewee.py:3023
def connect(self, reuse_if_open=False):
->  with self._lock:
        [...]
```
And reaches the point where the lock is acquired for the second time and deadlocks.

Solutions:
1. Use `threading.RLock` instead of `threading.Lock`. This fix the issue and still provide thread safety.
2. Open the connection if not opened in `Database.session_start()` before acquiring the lock.

Thanks, switched to RLock.
Issue: MariaDB 5 
Author: CaffeineLab
State: closed
Created: 2020-05-14 18:25:14

So I'm using MariaDB 5.?.? and every time I run I get a warning that it's unable to determine the MySQL version....  in the function it appears to only want to identify versions starting with a 1.  Not sure if this is an issue or if it's supposed to be a warning that you are using an old version of MariaDB so it's a bit unclear.  The function is def _extract_server_version(self, version) and the line in question is:

    if 'maria' in version:
        match_obj = re.search(r'(1\d\.\d+\.\d+)', version)

For now, I've removed the 1 from the re and it no longer gives me the warning.



10.0 was released in 2012... So are you using an 8+ year old version of MariaDB?

You can also suppress warnings if that's your preference: https://docs.python.org/3/library/warnings.html#temporarily-suppressing-warnings

Yes.  And I'm not happy about it either :)  So at this point, my opinion (and I am probably the only one with it) is that I think your warning is inaccurate and it should thereby be fixed because the MariaDB version **is** known .... it's just seriously outdated and shouldn't be used.  Oh - and the reason for it being old is it is what was shipped on the Synology DiskStation and I'm just using it temporarily so don't judge me too harshly.
Issue: how to prevent inserting NULL values and raising exception instead
Author: soufelhanafi
State: closed
Created: 2020-05-13 14:44:43

After running this code, the data will be inserted into the database. when I check the database, I find a row added with values: int_field=0 and other="" (empty string)

My question is how to throw exception in case of Null/empty insert.



import peewee as pw
from datetime import datetime

db = pw.MySQLDatabase('tests', user = 'root', password = 'root', host = 'localhost', port = 3306)


class Model(pw.Model):
    name = pw.CharField()
    created_at = pw.DateTimeField(default = datetime.now())
    other = pw.CharField(null = False)
    int_field = pw.IntegerField(null = False)

    class Meta:
        database = db
        db_table = 'nmodel'

Model.create_table()
Model.create(name = "tests")


I commented on your identical issue earlier.

https://github.com/coleifer/peewee/issues/2176#issuecomment-628016499
Issue: Insert data into database even the field are not allowing null values
Author: soufelhanafi
State: closed
Created: 2020-05-13 13:58:20

I have an issue with inserting data using peewee Model.create method.

after running the code below, the data will be inserted into the database with default values empty string for the **other field** and 0 for the **int_field**.

My question is how can I invoque an exception without inserting data into database.

import peewee as pw
from datetime import datetime

db = pw.MySQLDatabase('tests', user = 'root', password = 'root', host = 'localhost', port = 3306)


class Model(pw.Model):
    name = pw.CharField()
    created_at = pw.DateTimeField(default = datetime.now())
    other = pw.CharField(null = False)
    int_field = pw.IntegerField(null = False)

    class Meta:
        database = db
        db_table = 'nmodel'

Mode.create_table()
Model.create(name = "tests")


I am unclear what you are asking. Perhaps you have a mysql setting enabled that causes it to use default/empty values instead of raising an exception?

At any rate, this is not a peewee issue but a mysql question. Please take it to stackoverflow.
Issue: psycopg2.ProgrammingError: invalid dsn: invalid connection option "autoconnect"
Author: kmakwana
State: closed
Created: 2020-05-13 08:23:20

OS : Ubuntu 20.04
Python version : 3.8.2
Psycopg2 version : 2.8.5
Peewee version : 3.7.0

I am trying to do database connection but it gives me following error:
```
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/peewee.py", line 2600, in connect
    self._state.set_connection(self._connect())
  File "/usr/lib/python3/dist-packages/playhouse/pool.py", line 155, in _connect
    conn = super(PooledDatabase, self)._connect()
  File "/usr/lib/python3/dist-packages/playhouse/postgres_ext.py", line 431, in _connect
    conn = super(PostgresqlExtDatabase, self)._connect()
  File "/usr/lib/python3/dist-packages/peewee.py", line 3227, in _connect
    conn = psycopg2.connect(database=self.database, **self.connect_params)
  File "/usr/local/lib/python3.8/dist-packages/psycopg2/__init__.py", line 126, in connect
    dsn = _ext.make_dsn(dsn, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/psycopg2/extensions.py", line 176, in make_dsn
    parse_dsn(dsn)
psycopg2.ProgrammingError: invalid dsn: invalid connection option "autoconnect"
```
But if I remove `autoconnect=False` then there is no error.

Autoconnect was not present in 3.7.0: https://github.com/coleifer/peewee/blob/3.7.0/peewee.py#L2531-L2532
Issue: Reflection does not identify primary keys
Author: albireox
State: closed
Created: 2020-05-12 22:46:35

It seems that `generate_models` does not identify when a field is the primary key. Instead it creates a composite primary key with all the fields in the model.

If you can create a reproducible test-case I would be happy to investigate. As it is, the reflection module is already tested and I can assure you that it is not as fundamentally broken as all that.

https://github.com/coleifer/peewee/blob/3b1181a7cf727cc749a5fbb76f96602a3bb3325b/tests/reflection.py#L77-L443

Figure it out. The problem is that I was using a user that doesn't own the tables being reflected. The query that retrieves the primary keys uses the view `table_constraints` which, according to Postgresql

> The view table_constraints contains all constraints belonging to tables that the current user owns or has some privilege other than SELECT on.

A possible answer is explained [here](https://stackoverflow.com/questions/39379939/how-to-access-information-schema-foreign-key-constraints-with-read-only-user-in).
Issue: BitField variable name not accepting any name other than "flags"
Author: nisanb
State: closed
Created: 2020-05-12 15:11:30

Hi,
Given your example:
```
class Post(Model):
    content = TextField()
    flags = BitField()

    is_favorite = flags.flag(1)
    is_sticky = flags.flag(2)
    is_minimized = flags.flag(4)
    is_deleted = flags.flag(8)

```

When changing "flags" variable to any other variable, the fields are effectively not being saved in the database.

When querying to see the data, it always returns 0.

I don't see any such regression: 06c283159fa9917fda8836e2b2973fd38da0a029
Issue: pwiz generates non-auto_increment primary key field as AutoField
Author: zqch
State: closed
Created: 2020-05-11 04:04:11

I use pwiz to generate model codes, and I got `AutoField` rather than `IntegerField(primaty_key=True)` when the primary key field is not auto_increment, this will cause different results.
for example:
```py
"""
create table user_info (
  user_id int unsigned not null,
  name varchar(20) not null,
  primary key (user_id)
);
"""

class UserInfo(BaseModel):
  user_id = AutoField()
  name = CharField()

info = UserInfo.create(user_id=10, name='xxx')
print(info.user_id)  # 0

# result is different while using IntegerField

class UserInfo(BaseModel):
  user_id = IntegerField(primary_key=True)
  name = CharField()

info = UserInfo.create(user_id=10, name='xxx')
print(info.user_id)  # 10
```

I gather you're using MySQL.

For what it's worth, the first example where `user_id` is `AutoField` -- the `info.user_id` stored in the database is still "10" in both cases. The reason "0" gets assigned, however, is because Peewee thinks this is an auto-increment integer field. So it is trying to read the new pk value from the cursor -- and since this is *not* actually an autoincrement field, this is returned as 0.

You can verify that the correct value is still stored in either case, however:

```python
###
### Here UserInfo.user_id is an AutoField() instance.
###

In [2]: u = UserInfo.create(user_id=10, name='bar')                                                      

In [3]: UserInfo.get().user_id                                                                           
Out[3]: 10

In [4]: u.user_id                                                                                        
Out[4]: 0
```

In this case, you should probably go back through the auto-generated code and change the `AutoField()` to `IntegerField(primary_key=True)` where applicable.

You can see #2124 for some additional discussion.

> You can see #2124 for some additional discussion.

Thanks.
Issue: Unable to select/update model after migrating ForeignKeyField: RecursionError
Author: arel
State: closed
Created: 2020-05-09 15:48:10

I have a migration script that first adds a column and then initializes it. I am using MySQL. However, after adding the column, the model's `ForeignKeyField` is changed in a way that results in a recursion error by simply selecting or updating the new field:

```
RecursionError: maximum recursion depth exceeded
```

To reproduce it, imagine we have the two tables below, and I want to add a `ForeignKeyField` column to the `Rice` table:

```python
from peewee import Model, ForeignKeyField, TextField

class Bean(Model):
    name = TextField(default="Red")
    class Meta:
        database = db

class Rice(Model):
    name = TextField(default="Brown")
    bean = ForeignKeyField(Bean, null=True, backref='rices')  # <-- we want to add this field
    class Meta:
        database = db
```

Now, let's say I migrate the `Rice` table with something like this:

```python
migrator = MySQLMigrator(db)

migrate(
    migrator.add_column(
        Rice._meta.table_name,
        Rice.bean.column_name,
        Rice.bean
    ),
)
```

As a result of this line: https://github.com/coleifer/peewee/blob/7eb7498018b4265ce7e79c3e2b0b90a93d622d45/playhouse/migrate.py#L237 the value, `Rice.bean.name`, gets changed from `"bean"` to `"bean_id"`. This causes a `RecursionError` the next time we select that field.

```python
Rice.select().first()  # FAIL

~/.local/share/virtualenvs/acm-api-UVOmdwT4/lib/python3.7/site-packages/peewee.py in __set__(self, instance, value)
   4380 
   4381     def __set__(self, instance, value):
-> 4382         setattr(instance, self.field.name, value)
   4383 
   4384 

RecursionError: maximum recursion depth exceeded
```
Issue: Join fails on Python3.8
Author: ayyazdaniaryan
State: closed
Created: 2020-05-07 18:49:53

Hello,

I've recently used `peewee` for a new project, shying away after many years of `SQLAlchemy`. Neat package, works well, is pythonic, and small learning curve. Kudos!

I wanted to see anyone else though has noticed issues on the latest version of Python, 3.8, and peewee. I cannot seem to be able to do a `join` at all. Here's the error:

```
TypeError: 'NoneType' object is not callable
```

Here is how I produced this error:

**Python**: 3.8.2
**OS**: Mac OS X 10.14.6 
**DB**: Postgres 9.6.17
**Driver**: Psycopg2 2.8.5

Simple model structures, and a dead-simple join:

```python
class AAA:
   a = CharField(unique=True, index=True)

class BBB:
   a = CharField(null=False)
   b = ForeignKeyField(AAA, null=False)

class CCC:
   a = ForeignKeyField(AAA, null=False)
   b = ForeignKeyField(BBB, null=False)

entry_a = AAA.create(a="python")
entry_b = BBB.create(a="py", b=entry_a)
entry_c = CCC.create(a=entry_a, b=entry_b)

...

CCC.select(). # This works fine
CCC.select().join(BBB). # Throws the error
CCC.select().join(AAA) # Throws the error
```

My workaround for this was to downgrade my venv to Python3.7, and it's happy days. But it appears that Py3.8 has breaking changes for this package. Let me know if I'm doing something wrong, or if it's a known issue.

Your example code obviously won't run, as your classes do not extend `Model`. Neither can I tell why you are including a trailing "." after your CCC.select() and CCC.select().join(BBB).

I just ran the following code without any issues on Python 3.8.0:

```python

db = PostgresqlDatabase('peewee_test')

class BaseModel(Model):
    class Meta:
        database = db

class A(BaseModel):
    a = CharField(unique=True)

class B(BaseModel):
    a = CharField()
    b = ForeignKeyField(A)

class C(BaseModel):
    a = ForeignKeyField(A)
    b = ForeignKeyField(B)

db.create_tables([A, B, C])
a = A.create(a='1')
b = B.create(a='2', b=a)
C.create(a=a, b=b)

list(C.select())
q1 = C.select().join(B)
list(q1)
q2 = C.select().join(A)
list(q2)
db.drop_tables([A, B, C])
```
Issue: DecimalField wizhout max_digits
Author: daeeros
State: closed
Created: 2020-05-05 17:23:07

Why can not you specify max_digits, etc. In ordinary PhpPgadmin, this can be done and the column is simply called numeric without max_digits, etc.

![image](https://user-images.githubusercontent.com/34482855/81095923-5c5af380-8f0e-11ea-8a0b-c5e2ea562671.png)


Peewee requires that you be explicit w/the decimal field. If you dislike this functionality, then you can subclass it and override the `get_modifiers()` method.

```python

class NumericField(DecimalField):
    def get_modifiers(self):
           pass
```
Issue: Support for read slaves
Author: aniljaiswal
State: closed
Created: 2020-05-01 08:00:48

Hi @coleifer,

I'm using a master-slave(read-only) setup for our MySQL RDS. Currently, there's no straightforward way to specify the database connection on the basis of `SELECT` vs other query types. I stumbled upon your `read_slave` module from playhouse extension v2.10.0 release of peewee. 

Is it possible that I can use the same code to do it myself?

```python
from peewee import *


class ReadSlaveModel(Model):
    @classmethod
    def _get_read_database(cls):
        if not getattr(cls._meta, 'read_slaves', None):
            return cls._meta.database
        current_idx = getattr(cls, '_read_slave_idx', -1)
        cls._read_slave_idx = (current_idx + 1) % len(cls._meta.read_slaves)
        return cls._meta.read_slaves[cls._read_slave_idx]

    @classmethod
    def select(cls, *args, **kwargs):
        query = super(ReadSlaveModel, cls).select(*args, **kwargs)
        query.database = cls._get_read_database()
        return query

    @classmethod
    def raw(cls, *args, **kwargs):
        query = super(ReadSlaveModel, cls).raw(*args, **kwargs)
        if query._sql.lower().startswith('select'):
            query.database = cls._get_read_database()
        return query

```

taken from `https://github.com/coleifer/peewee/blob/2.10.2/playhouse/read_slave.py`

Also, I'd like to know if there are any future plans to bring back the support for read slaves.

The way to do this with Peewee 3 is probably to use the various context-managers:

http://docs.peewee-orm.com/en/latest/peewee/api.html#Database.bind_ctx

You can explicitly set `Query._database` as well on a one-off basis, but that's up to you.

Yep, I went with `DB.bind(models)`. Thanks for the response.

@aniljaiswal 
Stumbled upon this issue while looking for read slaves in peewee. 
may i do not completely comprehend how to use the context manager in here
How does `DB.bind(models)` work if you want to use the same model for both read and write operations, where my reads are executed on slaves and write on master ?

You basically create two connections, one for read and other for write. For read connection, you can specify the models to bind to the database.

Got it. Thanks
Issue: delete_instance is atomic? 
Author: theruziev
State: closed
Created: 2020-04-30 09:18:43

is it ok to delete_instance it's not atomic by design or I misunderstood code and documentation?

Transaction management is left up to the user. So if you want atomicity, then wrap your operations in the `atomic()` context manager.

http://docs.peewee-orm.com/en/latest/peewee/database.html#managing-transactions

Thanks for the answer
Issue: [Question] How to remove element from ArrayField
Author: daeeros
State: closed
Created: 2020-04-29 09:49:06

Hi, I apologize for the large number of questions. How can I remove an item from an ArrayField?

How to add, I seem to have found the function `from peewee import fn`
`fn.array_append ()`

But I did not find how to remove

https://www.postgresql.org/docs/9.3/functions-array.html#ARRAY-FUNCTIONS-TABLE

The issue tracker is not for "how do I..." questions. Ask those on:

* StackOverflow
* IRC
* Mailing list

http://docs.peewee-orm.com/en/latest/peewee/contributing.html#questions
Issue: [Question] How to add new value for ArrayField table
Author: daeeros
State: closed
Created: 2020-04-28 12:59:43

Hi, how i can add new value for arrayfield column? I try thet

`await db.execute(MarriagePlayer.update({MarriagePlayer.woman_childrens: MarriagePlayer.woman_childrens.append(1)}))`

But its not working(, i don't see  in documentation thet funcion

Read the postgresql array docs: https://www.postgresql.org/docs/9.1/arrays.html

You can use the concatenation operator, though it's pretty "fragile" and may complain about operators, in which case you'll need to use casts.
Issue: Invalid SQL is generated upon multiple joins of a table
Author: intruder777
State: closed
Created: 2020-04-27 12:36:20

I have strange problem with **peewee (v3.13.3)** when joining several tables in **Postgres (v12.2)**.

- DB structure SQL: https://gist.github.com/intruder777/f36a00b2009a1c9a8236c9f641819556
- DB models (generated by pwiz): https://gist.github.com/intruder777/18b800e3758df471d50fdf52cd05ef87
- Python run file: https://gist.github.com/intruder777/74477ace9d7df218f5f43c7368cd8c2b
- Output with traceback: https://gist.github.com/intruder777/8186e40dcf0a7cec4db4569bc1f63165

If I comment out line 27 in `run.py` - it works and generates valid SQL, but if I keep that line it generates invalid SQL.
Those files mentioned above are also attached:
[code_to_replicate_the_issue.zip](https://github.com/coleifer/peewee/files/4539593/code_to_replicate_the_issue.zip)


I'd be happy to look into this, but I'm not digging through all the craziness.

If you can produce a *minimal* example using simple models (e.g. my favorites are "User" / "Tweet") I'll reopen and investigate.
Issue: How to set comments for columns
Author: pch18
State: closed
Created: 2020-04-25 13:02:01

I tried to set the help_text field, it didn't seem to work

Peewee does not add comments to the schema.

Is this functionality you would be interested in having as part of peewee? 

I already have code I could easily adapt to a PR.

@pch18 A solution for you could look something like this:
``` python
import peewee as pw


class DatabaseComment(pw.Node):
    def __init__(self, obj, comment, obj_type):
        self.obj = obj
        self.comment = comment
        self.obj_type = obj_type

    def __sql__(self, ctx):
        ctx.literal('COMMENT ON ')
        ctx.literal(self.obj_type)
        ctx.literal(' ')
        with ctx.scope_values():
            if isinstance(self.obj, pw.Field):
                ctx.sql(self.obj.model).literal('.')
            ctx.sql(self.obj)

        ctx.literal(' IS ')
        ctx.sql(self.comment)
        return ctx

        
# >>> datababase.execute(DatabaseComment(SomeTable, 'Hello world', 'TABLE'))
```
Issue: Selecting from a union causes peewee.InterfaceError
Author: 8BitMate
State: closed
Created: 2020-04-23 15:47:16

Selecting from a union of two tables causes an InterfaceError.
If i write this query:
```python
q = (
    Data
    .select()
    .union(
        Data
        .select()
    ).select()
)
```
and call `q.execute()` I get the following error:
```
peewee.InterfaceError: Query must be bound to a database in order to call "execute".
```
If I call execute with a database like this: `q.execute(database)`, then it works fine.
I suspect the issue has something to do with the `select()` method of the `ModelCompoundSelectQuery` class not using the database from the BaseModel, but I don't have enough knowledge about the library to fix it myself. The issue remains for other methods that create a `ModelCompoundSelectQuery` object, like `except_()` and `union_all()` as well.

A fix for this issue would be greatly appreciated.

The issue is the final `select()` method.

```python

q1 = Data.select()
q2 = Data.select()
query = q1.union(q2)
```

I probably wasn't explaining the issue well enough, but focused on giving a minimal example to reproduce the issue.

What I have are two different queries producing two different tables. I then select from the union of these tables, and do some calculations in the select statement. It looks something like this:

```python
complex_query1 = ...
complex_query2 = ...
complex_query1.union(complex_query2).select(
    # Do calculations here based on this union
)
```
I want to be able to select from this union, as it allows me to do the calculations in a nice way. I know there are workarounds, but would be more clean to do something like this.

Then use the `select_from()` API instead.
Issue: How Update the required array in the database column (Integer)
Author: daeeros
State: closed
Created: 2020-04-23 10:57:27

```
await db.execute(MarriagePlayer.update({
            MarriagePlayer.statistics[0]: MarriagePlayer.statistics[0] + 1
        }).where(
            MarriagePlayer.chat_id == msg.receiver_id,
            MarriagePlayer.user_id << [msg.sender_id, p.loved]))
```

i try to update 0 array in column but i get error

> MarriagePlayer.statistics[0]: MarriagePlayer.statistics[0] + 1
> 
> TypeError: unhashable type: 'ObjectSlice'

how i can update it witzhou error?

Released in 3.13.3 https://github.com/coleifer/peewee/releases/tag/3.13.3