tortoise / tortoise/tortoise-orm

Regarding db_constraint, I think this is a bug

Open
#2,223 1 comment 1 reaction 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

Describe the bug
If the purpose of db_constraint is to avoid generating constraints in the database, then it does not seem to work as intended.

To Reproduce
Create the following data model and run the database migration:

class Feedback(Model):
    id = fields.IntField(primary_key=True)
    user = fields.ForeignKeyField('app.User', related_name='feedbacks', db_constraint=False) 
    title = fields.CharField(default='', max_length=50)

Expected behavior: No foreign key constraint exists in the database.
Actual behavior: Not only does a foreign key constraint get created, it is even more standardized and reliable than when db_constraint=True 😅
My actual requirement is to skip this physical database constraint entirely, while only retaining the relational annotation at the data model layer, with full support for prefetch_related, select_related and values() queries.

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

Reproduce the migration with the Feedback model shown, then trace how ForeignKeyField and db_constraint=False are handled during database schema generation. Confirm that no physical foreign-key constraint is created while prefetch_related, select_related, and values() continue to support the relationship.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.