Opened 8 years ago

Last modified 8 years ago

#27319 new Bug

Circular ForeignKeys between two unmanaged models produce incomplete migrations

Reported by: Jamie Bliss Owned by: nobody
Component: Migrations Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

From what I understand, makemigrations produces migrations for unmanaged models, and the state of those migrations are tracked, but no SQL is actually run.

The way that makemigrations handles circular ForeignKey references between two models is to produce two migrations. (This may only happen if the two models are in different applications.)

However, when both the models involved are unmanaged (managed = False), the second migration is not produced, leaving the migrations overall inconsistent with the models (and therefore, useless).

The workaround appears to be temporarily removing the managed flag, running the migrations, restoring the managed flag on the models and manually inserting it into the migrations.

Change History (3)

comment:1 by Tim Graham, 8 years ago

Just to be sure the ticket isn't interpreted incorrectly, could you provide the sample models?

in reply to:  1 comment:2 by Jamie Bliss, 8 years ago

Replying to Tim Graham:

Just to be sure the ticket isn't interpreted incorrectly, could you provide the sample models?

In a new project, I started the apps app1 and app2 and added them to INSTALLED_APPS.

app1/models.py:

from django.db import models

class Spam(models.Model):
    myegg = models.ForeignKey('app2.Egg')

    class Meta:
        managed = False

app2/models.py:

from django.db import models

class Egg(models.Model):
    myspam = models.ForeignKey('app1.Spam')

    class Meta:
        managed = False

In my case, only one migration in each application was created, and neither myegg nor myspam appears in either, just the primary keys.

comment:3 by Tim Graham, 8 years ago

Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

Thanks, I can reproduce the issue and I guess it's a bug. Probably not very high priority since circular foreign keys are an usual design.

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