10

I am trying create a new model with Django, but I keep running into the error Lookup failed for model referenced by field help.HelpDefinition.org: account.Organization. Organization has been imported. You can see the model below.

models.py

org = models.ForeignKey(Organization, unique=True)
help_type = models.CharField(max_length=255, choices=HELP_CHOICES)
help_content = models.TextField(blank=True)

This model has been successfully migrated previously. I dropped the table via psql in Postgres so that it could be recreated.

RHPT
  • 2,560
  • 5
  • 31
  • 43

3 Answers3

19

It happens when you change the target objects in relationship. Even if they have the same name and fields they are not the same objects. I had the same issue and deleting all previous migrations from migrations folder solved it.

warownia1
  • 2,771
  • 1
  • 22
  • 30
  • 1
    This sounds weird, but is in fact the way to do it. Modifications to model names seems something Django migrations do not support yet (or did not support yet in version 1.7.3). – Luís de Sousa Apr 03 '15 at 09:47
  • If you need to modify the name of a model, you may create an empty migration and use [`RenameModel`](https://docs.djangoproject.com/en/1.8/ref/migration-operations/#renamemodel) class. – warownia1 Jun 05 '15 at 12:05
4

You can also add as a dependency to the migration the last migration from the object's app. That did the trick for me.

class Migration(migrations.Migration):

dependencies = [
    (<app>, <last_migration_filename>),
...
SBD580
  • 691
  • 5
  • 3
0

My case was: moving away from South I deleted almost all migration files from several apps and applied makemigrations and migrate and later on I found out some forgotten migrations in one app and I tried to do the process (delete/makemigrations) only for this one app. But going back one step and recreating the migrations for ALL the apps fixed the issue for me.

obotezat
  • 1,041
  • 16
  • 20