Skip to content

Migrations not adding inherited fields until 2nd migration, and not making PKs generated #2251

Description

@markmiscavage

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions