akumoth/peewee-issues huggingface dataset:
Issue: PeeWee ForeignKeyField error. IntegrityError: (1215, 'Cannot add foreign key constraint')
Author: Ezyhoo
State: closed
Created: 2023-05-02 17:26:01

I have code like below:


```
import datetime

import peewee as pw
import os

my_db = pw.MySQLDatabase(database=os.getenv("DB_NAME"), host=os.getenv("DB_HOST"), port=os.getenv("DB_PORT"),
                            user=os.getenv("DB_USER"), passwd=os.getenv("DB_PASSWORD")


class MySQLModel(pw.Model):
    """A base model class that will use MySQL"""

    class Meta:
        database = my_db


class UnsignedBigAutoField(pw.BigAutoField):
    field_type = 'BIGINT UNSIGNED UNIQUE AUTO_INCREMENT'


class UnsignedAutoField(pw.AutoField):
    field_type = 'INT UNSIGNED UNIQUE AUTO_INCREMENT'


class User(MySQLModel):
    id = UnsignedBigAutoField(primary_key=True)
    alias = pw.CharField(max_length=16, unique=True, null=False)
    date_created = pw.DateTimeField(default=datetime.datetime.now)


class Ticket(MySQLModel):
    id = UnsignedBigAutoField(primary_key=True)
    user = pw.ForeignKeyField(User, backref="tickets", field="id", on_delete="RESTRICT", on_update="CASCADE")
    date_created = pw.DateTimeField(default=datetime.datetime.now)

my_db.create_tables([User, Ticket])

```
The last line of the code when creating ForeignKey produces error:

> peewee.IntegrityError: (1215, 'Cannot add foreign key constraint')


I checked the datatype(Bitint), and unique/non-null flag are all setup correctly for User table.

Couldn't figure out what went wrong here. I created the ForeignKey in MySqlWorkBench manually in Ticket table without a problem.

Any help would be appreciated.

It is probably not recommended to put the UNIQUE in the field definitions since these are already primary keys:

```python
class UnsignedBigAutoField(pw.BigAutoField):
    field_type = 'BIGINT UNSIGNED AUTO_INCREMENT'

class UnsignedAutoField(pw.AutoField):
    field_type = 'INT UNSIGNED AUTO_INCREMENT'
```

The issue is that Peewee sees your subclass of `BigAutoField` and infers that it should use BIGINT instead of BIGINT UNSIGNED. The fix is to override the `field_type` on the foreign-key, e.g.:

```python

class UnsignedForeignKey(pw.ForeignKeyField):
    field_type = 'BIGINT UNSIGNED'

class Ticket(MySQLModel):
    id = UnsignedBigAutoField(primary_key=True)
    user = UnsignedForeignKeyField(User, backref="tickets", field="id", on_delete="RESTRICT", on_update="CASCADE")
    date_created = pw.DateTimeField(default=datetime.datetime.now)
```
Issue: Fastapi db connection doc
Author: AndyJiangIsTaken
State: closed
Created: 2023-05-02 17:25:10

Hi, 
Here's the peewee doc I'm referring to:
https://docs.peewee-orm.com/en/latest/peewee/database.html#fastapi

According to the FastAPI doc,
the startup & shutdown events are for when the application, not request here:
https://fastapi.tiangolo.com/advanced/events/#startup-event

Let me know I'm missing something, new here.

The FastAPI docs have a thorough section, I probably need to just link it since your comment is correct and my docs may be misleading.

https://fastapi.tiangolo.com/advanced/sql-databases-peewee/#make-peewee-async-compatible-peeweeconnectionstate

Updated doc here: http://docs.peewee-orm.com/en/latest/peewee/database.html#fastapi
Issue: Calling save fails when PK is provided but record does not exist yet.
Author: keiranjprice101
State: closed
Created: 2023-05-01 12:02:11

In this example:

```python
class CustomerPeewee(BaseModel):
    email = CharField()

    class Meta:
        db_table = "customer"

customer = CustomerPeewee()
customer.id = 4
customer.email = "example@example.com"
customer.save()
```

No record is created and 0 is returned. If this is intended it feels counter intuitive as the docs seem to make no mention of this behaviour not being possible



The docs do make mention of this, there is in fact a section on this: http://docs.peewee-orm.com/en/latest/peewee/models.html#manually-specifying-primary-keys

At the most basic, though, this is all you need to add if you are doing this one-off:

```
customer.id = 4
customer.email = "example@example.com"
customer.save(force_insert=True)
```
Issue: Automatic Migrations Capability
Author: anuran-roy
State: closed
Created: 2023-04-27 15:58:44

Hey there!

Anuran this side! I'm a long time fan of peewee, been using it for a quite a few projects - thanks for this awesome tool, really saves a lot of time for someone who uses Django as well! 🚀 

Actually I was on the process of creating a custom script for automated migrations, since I couldn't find any automated tool for migrations, so I decided to create one. I was going through the codebase of peewee - it's pretty neat.

I wonder why an automated migrations haven't been added yet, especially given that `playhouse.migrate` does exist.

Can I go forward and develop an `AutoMigrator` object, that depends on the objects provided by `playhouse.migrate`?

>since I couldn't find any automated tool for migrations

You may not have looked very hard, there are several I'm aware of:

* https://github.com/klen/peewee_migrate
* https://github.com/aachurin/peewee_migrations
* https://github.com/timster/peewee-moves

I can't vouch for the fitness or suitability of any of the above, but they all seem to offer automatic migrations in some form.

>why automated migrations haven't been added

Because I don't believe they actually save much time, and that writing an explicit migration using the migrator module is preferable. Not everyone agrees with me, hence the above list. I have no plans on integrating such functionality into peewee, however.

I actually found only aachurin/peewee_migrations and it seems to be inactive, so I raised the issue. Thanks for the clarification!
Issue: Postgres error : Driver not installed, using peewee,docker and fastapi
Author: Master-Y0da
State: closed
Created: 2023-04-26 15:33:40

I have installed peewee 3.16.2 ,pyscopg2 66.0.2 and fastapi 0.95.1.

 I follow the steps in peewee docs about integrate fastapi apps, but showing up this error **peewee.ImproperlyConfigured: Postgres driver not installed!**

This is my config file in fastapi:
        
       def init_db():
         db = PostgresqlDatabase(
            'atlas_repos',
             user='atlas',
             password='password',
             host='localhost',
             port=5432)
     return db

this function is called from a main.py  in fastapi where I have a db.connect() function

Any help would be much apreciated.


Apparently you didn't install `psycopg2`, or for some reason Peewee is unable to import it. In your `main.py` try adding a line `import psycopg2 ; print(psycopg2)` - it should be easy to tell.

I intalled pyscogp2 not psycopg2 , really bad one hahaha thank you!!

You might've installed some malware or a miner, be careful, people typosquat pypi stuff.
Issue: How to "NOT" create index on a "unique" field
Author: eddysnjack
State: closed
Created: 2023-04-24 10:09:35

i have o model looking like this.

```
class User(BaseModel):
    id = BigIntegerField(unique=True)
    first_name = TextField()
    last_name = CharField(null=True)
    username = CharField(unique=True, null=True, index=False)
    phone = CharField(null=True)
    deleted = BooleanField(null=True)
    is_bot = BooleanField(null=True)
    bio = CharField(null=True)
    photo = TextField(null=True)
    roles = BigIntegerField()
```

After tables are created when i check db file with sqlstudio i saw "username" index on user table. I dont want this, it is unique but should be null as well. I'm getting "UNIQUE constraint failed: user.username" error when I try to update or create a user without a username second time. I believe that index is the cause.

<img width="870" alt="Screen Shot 2023-04-24 at 13 03 17" src="https://user-images.githubusercontent.com/38407276/233966648-e8504dc1-a15d-4fc8-974e-ad0f4ae42bba.png">




What database engine are you using?

> What database engine are you using?

how can i check that?

You presumably declared it yourself... SqliteDatabase, MySQLDatabase, PostgresqlDatabase...

At any rate it seems to be working fine for me:

```python
db = SqliteDatabase(':memory:')

class User(db.Model):
    username = TextField(null=True, unique=True)

User.create_table()

User.create(username=None)
User.create(username=None)
User.create(username='u1')
try:
    User.create(username='u1')
except IntegrityError:
    pass
else:
    assert False, 'expected integrity error not raised'

for u in User:
    print(u.username)
```

Correctly prints out:

```
None
None
u1
```

> You presumably declared it yourself... SqliteDatabase, MySQLDatabase, PostgresqlDatabase...

oh i see SqliteDatabase in this case. i misinterpreted "engine", sorry :)
Issue: insert_many method missing field values.
Author: frelion
State: closed
Created: 2023-04-24 06:07:36

peewee==3.16.0
Python 3.10.6

For the following code:
```
class TestModel(Model):
    class Meta:
        database = mysql_db
    test_field = CharField(null=True)

TestModel.create_table() 

insert_data = [
    {}, {
        'test_field': "hello"
    }
]

TestModel.insert_many(insert_data).execute()
```
Result:
```
id    test_field
1         NULL
2         NULL
```
The test_field field of the second record was not inserted.

The following is the modified code:
```
class TestModel(Model):
    class Meta:
        database = mysql_db
    test_field = CharField(null=True)

TestModel.create_table() 

insert_data = [{
    'test_field': None
},{
    'test_field': "hello"
}
]

TestModel.insert_many(insert_data).execute()
```
It worked
```
id    test_field
1         NULL
2         hello
```
Is it by design?
This goes against my instincts.
Thank you for working on such an easy-to-use ORM framework



http://docs.peewee-orm.com/en/latest/peewee/api.html?highlight=insert_many#Model.insert_many

Looks to be working fine:

```python
class Reg(db.Model):
    val = TextField(null=True)

Reg.create_table()
Reg.insert_many([{}, {'val': 'asdf'}], fields=[Reg.val]).execute()
print(list(Reg.select().tuples()))
# [(1, None), (2, 'asdf')]
```
Issue: Feature/filter none on where
Author: ponponon
State: closed
Created: 2023-04-20 10:35:57

```python
q = SeriesTable.select().where(
    None, None
)

```

If where is called for the first time and where has more than one None parameter, an error will occur:

```shell
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/ponponon/Desktop/code/me/gs_review_api/dev/peewee_in.py", line 32, in <module>
    q = SeriesTable.select().where(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ponponon/.local/share/virtualenvs/gs_review_api-0lO5qGKr/lib/python3.11/site-packages/peewee.py", line 755, in inner
    method(clone, *args, **kwargs)
  File "/Users/ponponon/.local/share/virtualenvs/gs_review_api-0lO5qGKr/lib/python3.11/site-packages/peewee.py", line 2156, in where
    self._where = reduce(operator.and_, expressions)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unsupported operand type(s) for &: 'NoneType' and 'NoneType'
```

So we filter the parameters of where and ignore those with the value None to avoid errors



I don't think silently ignoring arguments is a good plan, the issue is the caller passing `None` in the first place.
Issue:  pwiz generates detailed field attributes
Author: GuoGuo54
State: closed
Created: 2023-04-14 01:32:46

The peewee model generated by pwiz based on the database table is too simple， like：

class NameModel(Model):
    name = CharField(null=True)
    status = IntegerField(default=11, index=True)

    class Meta:
        table_name = 'name_model'


But generally we need more detailed field attributes，like (max_length, verbose):
 
class NameModel(Model):
    name = CharField(max_length=128, null=True, verbose_name='name')
    status = IntegerField(default=11, index=True, verbose_name='status')

    class Meta:
        table_name = 'name_model'



Going to pass. Column comments is mysql-specific, and the varchar max length is negligible in my view, since adding it to the introspected field doesn't change any behavior (and it usually isn't enforced anywhere).
Issue: How to avoid fetch-then-create pattern, when fk id already known?
Author: anentropic
State: closed
Created: 2023-04-13 16:33:13

New to Peewee... I'm sure this is easy but could not find an example in the docs

In a case like this:

```python
def start_game(user_id, subject: str) -> GameSession:
    user = User.get(User.id == user_id)
    game = GameSession.create(user=user, subject=subject)
    return game
```

How do I avoid first fetching the `User`, and instead create `GameSession` directly using the `user_id` already known for the foreign key field

Is there something like Django's `user__id` relation-spanning syntax, or implicit `_id` suffixed fields for foreign keys?

You can just do this:

```
game = GameSession.create(user=user_id, subject=subject)
```
Issue: Bitwise shift operators as functions?
Author: andycasey
State: closed
Created: 2023-04-12 05:22:22

Hi,

I know the Python operators `<<` and `>>` are overloaded in `peewee`. I'm wondering if there is some function (in `peewee.fn`) to use the [bitwise shift left/right operators in Postgresql](https://www.postgresql.org/docs/current/functions-bitstring.html). I've searched a lot but can't find anything myself.

My use case is that I am doing a bitwise OR operation on a byte from a bytearray like this:

```python

class Source(BaseModel):
    carton_flags = BigBitField()

# search by a carton flag `cf`
...
where=(
fn.get_byte(self.carton_flags, fn.div(cf, 8).cast("integer")) ^ (1 << fn.mod(cf, 8).cast("integer"))
)
```

And I'm running into Python errors (`TypeError: unsupported operand type(s) for <<: 'int' and 'Cast'`), which forced me to use some ugly-ass `SQL()` unless I could find the bitwise left/right functions. Right now my workaround solution is to use

```python
where=(fn.get_bit(self.carton_flags, cf) != 0)
```

but I'm wondering if there the bitwise left/right shift functions have been interfaced somewhere.

Thanks


You can look at how the `bin_and()`, `bin_or()`, and `__xor__()` methods on field/columns are implemented. There are no bitwish shifts at present, but it should be easy to add them for your case. You can do this as a standalone function, e.g.

```python
def lshift(lhs, rhs):
    return Expression(lhs, '<<', rhs)

# Usage.
fn.div(cf, 8).cast('integer') ^ lshift(1, fn.mod(cf, 8).cast('integer'))
```

Minimal example:

```python
class Reg(db.Model):
    value = IntegerField()

db.create_tables([Reg])

def lshift(lhs, rhs):
    return Expression(lhs, '<<', rhs)

for i in range(10):
    Reg.create(value=i)

query = Reg.select(Reg.value, lshift(Reg.value, 2).alias('shifted'))
for row in query:
    print(row.value, row.shifted)

# 0 0
# 1 4
# 2 8
# 3 12
# 4 16
# ...
```
Issue: Failed constraints raises IntegrityError but exceeding max_length raises DataEror with postgres
Author: TxMat
State: closed
Created: 2023-04-11 03:48:34

```py
# shipping info model
class ShippingInfo(BaseModel):
    id = peewee.AutoField(primary_key=True, unique=True)
    country = peewee.CharField(null=False)
    address = peewee.CharField(null=False)
    postal_code = peewee.CharField(null=False, max_length=7, constraints=[peewee.Check('length(postal_code) = 7')])
    city = peewee.CharField(null=False)
    province = peewee.CharField(null=False)
```

I don't know if it's intended but it makes catching for errors a bit trickier 

```py
try:
    shipping_info_instance = ShippingInfo.create(**shipping_info)
    order.shipping_info = shipping_info_instance
except peewee.IntegrityError, peewee.DataError:
    return errors.error_handler("orders", "invalid-fields",  "Les informations d'achat ne sont pas correctes"), 422
```

These errors are raised by postgres and passed on through psycopg2, which determines what error type to raise. Typically one would perform validation beforehand to eliminate something as simple as an over-length violation.
Issue: LIKE statement with postgres returns IndexError
Author: TxMat
State: closed
Created: 2023-04-11 03:37:04

The like statement inside the email field is causing peewee to return an index error when constructing the database

```py
class Order(BaseModel):
    id = peewee.AutoField(primary_key=True, unique=True)
    shipping_info = peewee.ForeignKeyField(ShippingInfo, backref='shipping_info', null=True)
    product = peewee.ForeignKeyField(Product, backref='product')
    email = peewee.CharField(null=True, constraints=[peewee.Check("email LIKE '%@%'")])
    paid = peewee.BooleanField(null=False, default=False)
    credit_card = peewee.ForeignKeyField(CreditCard, backref='credit_card', null=True)
    transaction = peewee.ForeignKeyField(Transaction, backref='transaction', null=True)
```

```
@pytest.fixture
    def client():
        app.config['TESTING'] = True
        client = app.test_client()
        # init db
        db.connect()
>       db.create_tables([Product, ShippingInfo, Transaction, CreditCard, Order, OrderProduct])

conftest.py:16: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
../../venv/lib/python3.11/site-packages/peewee.py:3425: in create_tables
    model.create_table(**options)
../../venv/lib/python3.11/site-packages/peewee.py:6865: in create_table
    cls._schema.create_all(safe, **options)
../../venv/lib/python3.11/site-packages/peewee.py:5967: in create_all
    self.create_table(safe, **table_options)
../../venv/lib/python3.11/site-packages/peewee.py:5818: in create_table
    self.database.execute(self._create_table(safe=safe, **options))
../../venv/lib/python3.11/site-packages/peewee.py:3244: in execute
    return self.execute_sql(sql, params)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <peewee.PostgresqlDatabase object at 0x7fe651b025d0>
sql = 'CREATE TABLE IF NOT EXISTS "order" ("id" SERIAL NOT NULL PRIMARY KEY, "shipping_info_id" INTEGER, "product_id" INTEGE...KEY ("credit_card_id") REFERENCES "creditcard" ("id"), FOREIGN KEY ("transaction_id") REFERENCES "transaction" ("id"))'
params = [], commit = None

    def execute_sql(self, sql, params=None, commit=None):
        if commit is not None:
            __deprecated__('"commit" has been deprecated and is a no-op.')
        logger.debug((sql, params))
        with __exception_wrapper__:
            cursor = self.cursor()
>           cursor.execute(sql, params or ())
E           IndexError: tuple index out of range

../../venv/lib/python3.11/site-packages/peewee.py:3236: IndexError
```

it seems that some other fields causes the indexError error but i can't precisely identify them

also i noticed the error because i'm migrating from sqlite to postgres 

The "%" need to be escaped:

```
email = peewee.CharField(null=True, constraints=[peewee.Check("email LIKE '%%@%%'")])
```
Issue: setup.py being deprecated
Author: crdoconnor
State: closed
Created: 2023-04-06 11:34:12

This message is being displayed when pip installing peewee on python version 3.11.2:

```
DEPRECATION: peewee is being installed using the legacy 'setup.py install' method,
because it does not have a 'pyproject.toml' and the 'wheel' package is not installed.
pip 23.1 will enforce this behaviour change. A possible replacement is to enable
the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
```

What version of peewee are you using? I think this should be fixed since we have a magic pyproject toml file now.

3.16.0, installed with python 3.11.2:

```
root@6b1a08642656:/src# /venv/bin/pip install peewee==3.16.0 
Collecting peewee==3.16.0
  Using cached peewee-3.16.0.tar.gz (866 kB)
  Preparing metadata (setup.py) ... done
Installing collected packages: peewee
  DEPRECATION: peewee is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559
```

I think your pyproject needs to have all of the details included.

Oh I might have forgotten to add it to the manifest so it didn't get bundled! If you are able to look inside that cached peewee-3.16.0.tar.gz can you see if pyproject.toml is present?

That might be it. Yes, it's missing.

Thanks I'll get that added to the manifest. It's fine to ignore the warning in the meantime.
Issue: Create aware datetime objects for TimestampField
Author: IBuckton
State: closed
Created: 2023-04-06 00:55:51

**Description**
With `TimestampField(utc=True)` it returns a naive datetime object. This is not recommended as they are treated by many datetime methods as local times.

**Expected behaviour**
Return an aware datetime object to avoid confusion, per the recommendation in [datetime docs](https://docs.python.org/3.11/library/datetime.html#datetime.datetime.utcfromtimestamp)

**Suggested change**
```python
    if self.utc:
-       value = datetime.datetime.utcfromtimestamp(value)
+       value = datetime.datetime.fromtimestamp(value, datetime.timezone.utc)
```

You can't compare naive and tz-aware datetimes, so this is a non-starter unfortunately. The idea is that your naive datetime *is* already in UTC so the tzinfo is meaningless and provides flexibility -- that is *also* why `utc=False` by default. We can't be switching the return value between naive and tz-aware based on the value of the `utc` parameter.

My suggestion would be to subclass `TimestampField` and override the `python_value()` if you wish, e.g.

```python
class UTCTimestampField(TimestampField):
    def __init__(self, **kwargs):
        kwargs['utc'] = True
        super(UTCTimestampField, self).__init__(**kwargs)
    def python_value(self, value):
        value = super(UTCTimestampField, self).python_value(value)
        if value is not None:
            return value.replace(tzinfo=datetime.timezone.utc)
```
Issue: Ae configurable default
Author: paetling
State: closed
Created: 2023-03-26 20:10:10

## Context
Make it so that we can configure the type of the id that is created

Is there a reason you are not just using:

```python
class Base(Model):
    id = BinaryUUIDField(primary_key=True, ...)

class MyModel(Base):
    # MyModel.id will be a binary uuid field
```

This change seems redundant.
Issue: peewee-value-returns-to-its-default-after-few-seconds-bug
Author: instasck
State: closed
Created: 2023-03-21 18:26:54

https://stackoverflow.com/questions/75804965/peewee-value-returns-to-its-default-after-few-seconds-bug

Can you explain this ?

OK ISSUES IS
That a pointer to the database is not refreshing automatically, it pointing to local data in memory so holding old data.

Don't spam the issues -- please try to have a bit of patience.

Well it took me 2 hours before I found the issue.
it is not clear about refreshing data from the doc...