tortoise / tortoise/tortoise-orm
Migrations not adding inherited fields until 2nd migration, and not making PKs generated
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
I rebuilt all my migrations recently from aerich to the new built in migrations. My initial migration that's generated (overall or for any new model added) doesn't include any inherited fields from Abstract models. When I run a second makemigrations command it then creates a new migration that adds all the inherited abstract model fields, or in the case of inherited PKs it drops the pk field and adds it again. When both migrations are run all of my PKs that should be autogenerated aren't getting a sequence created and added in the SQL.
To Reproduce
class BaseModel(Model):
id = fields.BigIntField(pk=True, generated=True, source_field="id", index=True)
created = fields.DatetimeField(
auto_now_add=True, source_field="created", index=True
)
modified = fields.DatetimeField(auto_now=True, source_field="modified", index=True)
deleted = fields.BooleanField(
null=True, source_field="deleted", default=False, index=True
)
class Meta:
abstract = True
class NewModel(BaseModel):
name = fields.CharField(max_length=255)
Run makemigrations twice, first run will generate table but miss abstract fields, second run will add missing abstract fields. Check table in postgres and see that the NewModel table and id field are missing a newmodel_id_seq.
Expected behavior
Abstract fields should be picked up on a single run as it worked in aerich. Fields defined as pk=True and generated=True should autogenerate a PK upon insert in SQL.
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 issue with the BaseModel and NewModel definitions, then run makemigrations twice and inspect the generated migrations and PostgreSQL SQL. Done means one migration includes inherited abstract fields and creates the generated primary-key sequence without requiring a second run.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, python
- Domain
- backend, databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100