tortoise / tortoise/tortoise-orm

Defining an IntField as pk=True should not change the generated value

Open
#1,369 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Is your feature request related to a problem? Please describe.
Well it's simply not natural. I started to use tortoise recently along with my fellow mate in a project and we were confused as to why PKs were being generated when we did not told tortoise to do so. It was just not what we expected.. we've spent couple hours just to figure out that BigIntField was overriding the default Field value of generate=False to True (I assume its the same with all other int fields, please do not do that! It does not feel natural)

Describe the solution you'd like
Ideally, fields such as BigIntField should only be used for the type of the field and not do some hidden black magic in the background such as flipping default Field values.

Describe alternatives you've considered
Manually setting generated=False in the BigIntField constructor, but it is not a desired comportment! As mentioned earlier, we were confused for many hours trying to figure out why an id was automatically generated when it wasn't told to do so.

Additional context
In my opinion, creating a PK field should not make the generated value to flip to true, that's not how it works in SQL, you have to define that it is generated. It should go along the same lines here: PK does not mean its auto-generated, it means it's the primary key.. I agree that in many cases people tends to go for an auto-generated PK because it is easier, but in the end, pk != generated.

Also, nothing in the documentation says that defining any IntField to pk=True would also make the generated value turn to True

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing BigIntField and the base Field constructor to confirm where pk=True changes generated. Check the integer-field behavior and existing documentation, then add coverage showing that primary-key declaration does not implicitly enable generation; update the documentation to describe the intended distinction.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.