tortoise / tortoise/tortoise-orm

Which field attributes only matter for schema generation?

Open
#1,850 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When I discovered that TextField doesn't currently allow unique=True, it got me thinking... does it matter?

I don't use Tortoise to generate my schema, so there's no point setting field attributes that only matter for schema generation — other than as a form of documentation, which can be accomplished just as well in a comment.

So I did some skimming of the code and confirmed that index does, indeed, only matter for the sake of schema generation.


So, the next question is — which field attributes affect Tortoise behavior, and which only matter for schema generation? And might it be useful to describe that in the docs?

After some more skimming, I reached the following conclusions:

  • Attributes that affect Tortoise behavior: auto_now, auto_now_add, default, generated, max_length, null, pk
  • Attributes that only matter for schema generation: index, on_delete, unique[1]
  • Attributes that only matter for schema generation, but are required by the field constructor so you have to specify them anyway: max_digits, decimal_digits

Note: I skipped a few that don't currently matter to me.


Would the authors like to comment on this topic, and also, would they appreciate a patch to add something like this to the docs?


[1] The exception is if configuring a ForeignKeyField or OneToOneField with to_field attribute pointed at a non-pk field. In that case, you do need to specify unique=True on the target field or Tortoise will complain.

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

No files or tests are named. Start by reviewing the field definitions and schema-generation documentation, focusing on the listed attributes and the TextField and relationship exceptions; verify the runtime-versus-schema distinction, then document it clearly, including the to_field/unique=True exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 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.