Opened 6 years ago

Closed 6 years ago

Last modified 6 years 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: dev
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 6 years ago.
The Git repository with stages of the bug appearance

Download all attachments as: .zip

Change History (4)

by Victor Porton, 6 years ago

Attachment: bug.tar.gz added

The Git repository with stages of the bug appearance

comment:1 by Mariusz Felisiak, 6 years ago

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 by Victor Porton, 6 years ago

Adding migrations.AlterModelBases() should be done automatically.

comment:3 by Mariusz Felisiak, 6 years ago

Yes, see comment.

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