Closed sakuragasaki46 closed 5 days ago
Update
Passing generated=False
as field parameter for the PK makes things work.
I updated the lines:
class User(Model): # or whatever
id = fields.BigIntField(primary_key=True, default=new_id)
to:
class User(Model): # or whatever
id = fields.BigIntField(primary_key=True, default=new_id, generated=False)
In documentation we have note about it
If this is used on an Integer Field, ``generated`` will be set to ``True`` unless you explicitly pass ``generated=False`` as well.
generated=True
enforces generation of pk value on db side, glad you figured it out
Describe the bug I have a function that generates Snowflakes (i.e. long integers based on timestamp) for use as PK's in the database. When I call
.create()
on a database model, looks like the function (passed as callable in thedefault=
parameter of the BigIntField) is ignored altogether and a serial number (like 1, 2, 3...) is generated instead in the backend.Proof:
To Reproduce I have a custom function to generate a Snowflake (64 bit integer), like this:
I have this line I copy paste in every Model as a primary key field (please note new_id passed as callable):
I did not supply
id = new_id()
as argument tocreate()
, therefore creation looks like this:Expected behavior When calling
Model.create()
with all the appropriate fields, the PK should be populated with a snowflake value.Additional context I use PostgreSQL (asyncpg) as the backend, Quart web framework, Python 3.12. I use the Aerich migration tool.