Opened 10 years ago
Closed 10 years ago
#22944 closed Bug (fixed)
Migrate doesn't like ForeignKey being reassigned to a different app's model
Reported by: | Owned by: | Andrew Godwin | |
---|---|---|---|
Component: | Migrations | Version: | 1.7-rc-2 |
Severity: | Release blocker | 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
Hi,
Django 1.7 RC1 doesn't like it when I try to migrate an app's models.
I have a couple of models in App "Background". Simplified example:
class Family(models.Model): name = models.CharField( _("name"), max_length=255, blank=True, null=True, ) order = models.PositiveSmallIntegerField( _("order"), default=0, ) parent = models.ForeignKey( 'self', blank=True, null=True, class ImageLayer(models.Model): family = models.ForeignKey( Family, verbose_name=_("Family Name"), related_name='background_family_name', null=True, blank=True, )
I have another separate App, "Styles" with a similar Family model:
class StyleFamily(models.Model): name = models.CharField( _("name"), max_length=255, blank=True, null=True, ) order = models.PositiveSmallIntegerField( _("order"), default=0, ) parent = models.ForeignKey( 'self', blank=True, null=True, )
Now, I try to delete my "Family" model from the Background App, and reassign the family field to 'styles.StyleFamily" as:
class ImageLayer(models.Model): family = models.ForeignKey( 'styles.StyleFamily', verbose_name=_("Family Name"), related_name='background_family_name', null=True, blank=True, )
Then I run manage.py makemigration and manage.py migrate
I get the following:
bobby:engine mozumder$ ./manage.py makemigrations Migrations for 'background': 0002_auto_20140702_1621.py: - Remove field parent from family - Delete model Family - Alter field family on imagelayer - Alter field family on stack bobby:engine mozumder$ ./manage.py migrate Operations to perform: Synchronize unmigrated apps: grappelli Apply all migrations: sessions, styles, background, blocks, borders, defaults, effects, structure, transform, sites, contenttypes, admin, typography, content, animation, auth, colors, redirects, layout, text, articles, categories Synchronizing apps without migrations: Creating tables... Installing custom SQL... Installing indexes... Running migrations: Applying background.0002_auto_20140702_1621...Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/config.py", line 152, in get_model return self.models[model_name.lower()] KeyError: 'family' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/state.py", line 79, in render model = self.apps.get_model(lookup_model[0], lookup_model[1]) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/registry.py", line 190, in get_model return self.get_app_config(app_label).get_model(model_name.lower()) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/apps/config.py", line 155, in get_model "App '%s' doesn't have a '%s' model." % (self.label, model_name)) LookupError: App 'background' doesn't have a 'family' model. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "./manage.py", line 10, in <module> execute_from_command_line(sys.argv) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line utility.execute() File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/__init__.py", line 377, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv self.execute(*args, **options.__dict__) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/base.py", line 337, in execute output = self.handle(*args, **options) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 160, in handle executor.migrate(targets, plan, fake=options.get("fake", False)) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 62, in migrate self.apply_migration(migration, fake=fake) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/executor.py", line 96, in apply_migration migration.apply(project_state, schema_editor) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/migration.py", line 107, in apply operation.database_forwards(self.app_label, schema_editor, project_state, new_state) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 118, in database_forwards from_model = from_state.render().get_model(app_label, self.model_name) File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/django/db/migrations/state.py", line 89, in render model=lookup_model, ValueError: Lookup failed for model referenced by field background.ImageLayer.family: background.Family
It seems it doesn't like it when I reassign the "family" field to a ForeignKey outside the App. This happens even with empty "Family" and "styles.Family" models.
I avoid this situation by rebuilding the database from scratch and calling in fixtures. I think this is probably a bug?
This is with Django 1.7 RC1, Python 3.4.0, and Postgres 9.3.4
Anyways, let me know if there is a workaround... thank you!
-bobby
Change History (11)
comment:1 by , 10 years ago
comment:3 by , 10 years ago
I have same problem after commit fe5f29e.
It's not related/duplicate of #22970. :(
comment:4 by , 10 years ago
I get the same error on 1.7 RC1. I have two apps, both created pre 1.7, that had no reference to each other. I made migrations for both, and applied them (Faked correctly). Then I changed the foreign keys in one app to point to a model in the second app. I get ValueError: Lookup failed for model referenced by field tracking.PropertyDescription.verified_by: employees.Employee. I asked a question on the Google group https://groups.google.com/forum/#!topic/django-users/9wgCcuSdVmo, which specifies a bit more detail, but today I created a brand new project to test this and received the exact same error. I don't know if it's exactly the same issue, but it happens consistently. I'll be glad to file another bug if it isn't the same issue.
comment:5 by , 10 years ago
Severity: | Normal → Release blocker |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:6 by , 10 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Looks like the cause of this is the autodetector not knowing that foreign key alters should come before deletion of the model they used to point to. Since there are no steps to reproduce or example project, I'll fix that bug and mark this as closed and then if it still persists it can be reopened.
comment:7 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
comment:9 by , 10 years ago
I get the same error on 1.7 RC2. Here is my code https://github.com/shultais/bug22944
Error with FK and M2M too.
comment:10 by , 10 years ago
Resolution: | fixed |
---|---|
Status: | closed → new |
Version: | 1.7-rc-1 → 1.7-rc-2 |
Confirmed. Reopening.
comment:11 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
OK, this bug is different to the one originally reported here, and looks to be the same as #23100. Re-closing this and transferring to that one as the main tracking bug.
Oh I'll add that the initial "Family" model is based on mixins from another App, engine/globals.py:
with engine/globals.py having the following:
Not sure if that affects expected behavior...