tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.57k stars 377 forks source link

0.21.0 throws "IntegrityError: NOT NULL constraint failed" due to usage of 'pk' to indicate primary key #1625

Closed andrewgreig closed 4 months ago

andrewgreig commented 4 months ago

With tortoise-orm version 0.21.0, code that worked with 0.20.1 throws an exception when saving an object to the database.

Traceback (most recent call last):
  File "/home/andrew/tortoise/.venv_new/lib/python3.11/site-packages/tortoise/backends/sqlite/client.py", line 34, in translate_exceptions_
    return await func(self, query, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/tortoise/.venv_new/lib/python3.11/site-packages/tortoise/backends/sqlite/client.py", line 117, in execute_insert
    return (await connection.execute_insert(query, values))[0]
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/tortoise/.venv_new/lib/python3.11/site-packages/aiosqlite/core.py", line 194, in execute_insert
    return await self._execute(self._execute_insert, sql, parameters)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/tortoise/.venv_new/lib/python3.11/site-packages/aiosqlite/core.py", line 129, in _execute
    return await future
           ^^^^^^^^^^^^
  File "/home/andrew/tortoise/.venv_new/lib/python3.11/site-packages/aiosqlite/core.py", line 102, in run
    result = function()
             ^^^^^^^^^^
  File "/home/andrew/tortoise/.venv_new/lib/python3.11/site-packages/aiosqlite/core.py", line 74, in _execute_insert
    cursor = self._conn.execute(sql, parameters)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlite3.IntegrityError: NOT NULL constraint failed: tournament.id

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/andrew/tortoise/tortoise_test.py", line 17, in <module>
    run_async(main())
  File "/home/andrew/tortoise/.venv_new/lib/python3.11/site-packages/tortoise/__init__.py", line 624, in run_async
    loop.run_until_complete(coro)
  File "/usr/lib/python3.11/asyncio/base_events.py", line 654, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/home/andrew/tortoise/tortoise_test.py", line 14, in main
    await tournament.save()
  File "/home/andrew/tortoise/.venv_new/lib/python3.11/site-packages/tortoise/models.py", line 979, in save
    await executor.execute_insert(self)
  File "/home/andrew/tortoise/.venv_new/lib/python3.11/site-packages/tortoise/backends/base/executor.py", line 227, in execute_insert
    insert_result = await self.db.execute_insert(self.insert_query, values)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/andrew/tortoise/.venv_new/lib/python3.11/site-packages/tortoise/backends/sqlite/client.py", line 38, in translate_exceptions_
    raise IntegrityError(exc)
tortoise.exceptions.IntegrityError: NOT NULL constraint failed: tournament.id

To Reproduce

models.py

from tortoise.models import Model
from tortoise import fields

class Tournament(Model):
    id = fields.IntField(pk=True)
    name = fields.TextField()

    def __str__(self):
        return self.name

tortoise_test.py

from tortoise import Tortoise, run_async
from models import Tournament

async def init():
    await Tortoise.init(
        db_url="sqlite://db.sqlite3",
        modules={"models": ["models"]}
    )
    await Tortoise.generate_schemas()

async def main():
    await init()
    tournament = Tournament(name="New Tournament")
    await tournament.save()
    print(tournament)

run_async(main())

Expected behavior Other than the deprecation warning about the use of 'pk', the code should still run and not throw an exception, as per #1621

Additional Information Changing the model definition to use 'primary_key' instead of 'pk' works around the error.

Package           Version
----------------- -------
aiosqlite         0.17.0
annotated-types   0.7.0
iso8601           1.1.0
pip               24.0
pydantic          2.7.1
pydantic_core     2.18.2
pypika-tortoise   0.1.6
pytz              2024.1
setuptools        68.1.2
tortoise-orm      0.21.0
typing_extensions 4.12.0
PieceOfGood commented 4 months ago

New tables are created without the AUTOINCREMENT option for PRIMARY KEY, if the id is defined according to the old style, indicating like id = fields.IntField(pk=True, index=True)

abondar commented 4 months ago

That's a pity, I though I fixed that bug during development, but for some reason it wasn't commited

I'll fix it and release as 0.21.1

abondar commented 4 months ago

Published 0.21.1