tortoise / tortoise/tortoise-orm

Constraints defined within Meta are not applied when performing data migrations.

Open
#2,272 3 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

Describe the bug
Constraints are defined in the Meta class of the data model, and the native Tortoise migration tool is used to perform data migration. However, the constraints are not created as expected in the resulting database table.

To Reproduce

class Like(Model):
    id = IntField(primary_key=True)
    types = CharField(default='news', max_length=10)
    user_id = IntField(default=0)
    art_id = IntField(default=0) 
    class Meta:
        table = 'like'
        constraints = [UniqueConstraint(fields=('types', 'user_id', 'art_id'),  name='unique_like')]

*The Tortoise‑ORM version is 1.1.8
Expected behavior
A unique index named unique_like was supposed to be created in the database, but nothing appears in the database.
There are no exceptions or error hints during data migration.

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 with the native Tortoise migration tool's handling of constraints declared in a model's Meta class, using the Like model and its unique_like constraint as the reproduction. Run a data migration against the supported database setup and verify that the resulting like table contains the expected unique index without errors.

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
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.