tortoise / tortoise/tortoise-orm
default value on PK field does not work
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
Describe the bug
default value on PK field does not work
To Reproduce
MODEL:
class User(Model):
id = fields.BigIntField(pk=True, default=6)
Action:
user = User()
user.save()
Report:
Traceback (most recent call last):
File "handle_request", line 96, in handle_request
if TYPE_CHECKING:
File "/Users/apple/SynologyDrive/code/rena_sanic/account/views.py", line 18, in get
await user.save()
File "/Users/apple/opt/anaconda3/envs/sanic/lib/python3.9/site-packages/tortoise/models.py", line 960, in save
await executor.execute_insert(self)
File "/Users/apple/opt/anaconda3/envs/sanic/lib/python3.9/site-packages/tortoise/backends/base/executor.py", line 226, in execute_insert
insert_result = await self.db.execute_insert(self.insert_query, values)
File "/Users/apple/opt/anaconda3/envs/sanic/lib/python3.9/site-packages/tortoise/backends/base_postgres/client.py", line 34, in _translate_exceptions
return await self._translate_exceptions(func, *args, **kwargs)
File "/Users/apple/opt/anaconda3/envs/sanic/lib/python3.9/site-packages/tortoise/backends/asyncpg/client.py", line 86, in _translate_exceptions
raise IntegrityError(exc)
tortoise.exceptions.IntegrityError: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, 0, 86, 0, null, 0, 0, 2022-10-05 04:39:59.639835+00, 2022-10-05 04:39:59.639953+00).
Expected behavior
A clear and concise description of what you expected to happen.
Additional context
Add any other context about the problem here.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the model and save sequence shown in the issue against PostgreSQL. Read tortoise/models.py around save and tortoise/backends/base/executor.py around execute_insert, then verify that the configured primary-key default is included in the insert rather than producing a null id; no test file is identified in the report.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, python
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100