Opened 4 months ago

Closed 4 months ago

Last modified 4 months ago

#30513 closed Bug (duplicate)

Impossible migration (with a change of model base) is not noticed.

Reported by: Victor Porton Owned by: nobody
Component: Migrations Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

  1. Extract the attached Git repository. (It contains among other migrations automatically created with makemigrations, one of the migrations was made with choosing the option 2) Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration).)
  1. Try to migrate.

You see the error:

django.db.utils.OperationalError: foreign key mismatch - "mytest_project" referencing "mytest_token"

The Django bug is that this error is noticed too late: on the stage of migrate not where it should have been noticed, the stage of makemigrations.

The bug appears when I try to replace (in two stages as described by tags below, because Django does not allow to do it in one stage):

class Token(models.Model):
     pass

with

class BaseToken(models.Model):
    pass


class Token(BaseToken):
    base = models.OneToOneField(BaseToken, on_delete=models.CASCADE, primary_key=True)

This happens because this change unnoticed by Django replaces what is used as the primary key for Token.

Note that sequential stages of change in the Git repository are marked by tags before-change, change-middle, after-change.

Attachments (1)

bug.tar.gz (20.0 KB) - added by Victor Porton 4 months ago.
The Git repository with stages of the bug appearance

Download all attachments as: .zip

Change History (4)

Changed 4 months ago by Victor Porton

Attachment: bug.tar.gz added

The Git repository with stages of the bug appearance

comment:1 Changed 4 months ago by felixxm

Resolution: duplicate
Status: newclosed
Summary: Impossible migration (with a change of model base) is not noticedImpossible migration (with a change of model base) is not noticed.
Version: 2.2master

Adding migrations.AlterModelBases() to a migration operations should solve this issue.

Marking as a duplicate of #23521.

comment:2 Changed 4 months ago by Victor Porton

Adding migrations.AlterModelBases() should be done automatically.

comment:3 Changed 4 months ago by felixxm

Yes, see comment.

Note: See TracTickets for help on using tickets.
Back to Top