[1.8] Odd error on makemigrations when moving model to another application

39 views
Skip to first unread message

Alex Heyden

unread,
Nov 2, 2015, 5:34:37 PM11/2/15
to Django users
Traceback first:

./manage.py makemigrations
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
    utility.execute()
  File "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 343, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py", line 125, in handle
    migration_name=self.migration_name,
  File "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 43, in changes
    changes = self._detect_changes(convert_apps, graph)
  File "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py", line 110, in _detect_changes
    self.old_apps = self.from_state.concrete_apps
  File "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/db/migrations/state.py", line 170, in concrete_apps
    self.apps = StateApps(self.real_apps, self.models, ignore_swappable=True)
  File "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/db/migrations/state.py", line 248, in __init__
    raise ValueError(msg.format(field=operations[0][1], model=lookup_model))
ValueError: Lookup failed for model referenced by field sdd.Documentation.opportunity: sdd.......governance.models.Opportunity

Needless to say, sdd.......governance.models.Opportunity is not a valid path to anything.

The Opportunity model was moved from the sdd application to the governance application. Documentation has a foreign key to Opportunity. The Python itself is correct. It used to be a direct reference to the class in the module, now it's an imported reference. All of the foreign keys are handled in the same way. Nothing is done by string.

I'm ok with a one-off solution for the migration, but I'm more interested in why the model path is so far off and what I might have done to cause it.

Alex Heyden

unread,
Nov 2, 2015, 5:56:31 PM11/2/15
to django...@googlegroups.com
I tried putting the model back in its original module with some much smaller changes, but I'm getting the same error. The error is nonsense in the current context.

Is there some intermediate state saved somewhere when you try to run makemigrations?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d5d6fcf9-8b76-4448-81fb-e38765b66d03%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mike Dewhirst

unread,
Nov 3, 2015, 12:07:35 AM11/3/15
to django...@googlegroups.com
On 3/11/2015 9:55 AM, Alex Heyden wrote:
> I tried putting the model back in its original module with some much
> smaller changes, but I'm getting the same error. The error is nonsense
> in the current context.
>
> Is there some intermediate state saved somewhere when you try to run
> makemigrations?

Not as far as I can tell. I too have had difficulty moving models
between apps. It is in my too-hard basket for the moment. However ...

When I try again I'll create the model-to-be-moved in its new app and
get that working properly. I would definitely do a makemigration but
edit the migration file prior to migrating to use a forwards_func()
method to get the data across and entrenched in its proper relations
with other models on creation. Finally, when all is working I'd bravely
drop the old model in a separate migration.

hth

Mike

>
> On Mon, Nov 2, 2015 at 4:34 PM, Alex Heyden <minds...@gmail.com
> <mailto:minds...@gmail.com>> wrote:
>
> Traceback first:
>
> ./manage.py makemigrations
> Traceback (most recent call last):
> Â File "./manage.py", line 10, in <module>
> Â Â execute_from_command_line(sys.argv)
> Â File
> "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/core/management/__init__.py",
> line 351, in execute_from_command_line
> Â Â utility.execute()
> Â File
> "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/core/management/__init__.py",
> line 343, in execute
> Â Â self.fetch_command(subcommand).run_from_argv(self.argv)
> Â File
> "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/core/management/base.py",
> line 394, in run_from_argv
> Â Â self.execute(*args, **cmd_options)
> Â File
> "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/core/management/base.py",
> line 445, in execute
> Â Â output = self.handle(*args, **options)
> Â File
> "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py",
> line 125, in handle
> Â Â migration_name=self.migration_name,
> Â File
> "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py",
> line 43, in changes
> Â Â changes = self._detect_changes(convert_apps, graph)
> Â File
> "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/db/migrations/autodetector.py",
> line 110, in _detect_changes
> Â Â self.old_apps = self.from_state.concrete_apps
> Â File
> "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/db/migrations/state.py",
> line 170, in concrete_apps
> Â Â self.apps = StateApps(self.real_apps, self.models,
> ignore_swappable=True)
> Â File
> "/home/me/.virtualenvs/fluent/local/lib/python2.7/site-packages/django/db/migrations/state.py",
> line 248, in __init__
> Â Â raise ValueError(msg.format(field=operations[0][1],
> model=lookup_model))
> ValueError: Lookup failed for model referenced by field
> sdd.Documentation.opportunity: sdd.......governance.models.Opportunity
>
> Needless to say, sdd.......governance.models.Opportunity is not a
> valid path to anything.
>
> The Opportunity model was moved from the sdd application to the
> governance application. Documentation has a foreign key to
> Opportunity. The Python itself is correct. It used to be a direct
> reference to the class in the module, now it's an imported
> reference. All of the foreign keys are handled in the same way.
> Nothing is done by string.
>
> I'm ok with a one-off solution for the migration, but I'm more
> interested in /why/Â the model path is so far off and what I might
> have done to cause it.
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/d/msgid/django-users/d5d6fcf9-8b76-4448-81fb-e38765b66d03%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CA%2Bv0ZYWOTbagf0NUYy9%2BrCwQNPjaDJzdZAaOgCkm7ox7%2BXY60A%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CA%2Bv0ZYWOTbagf0NUYy9%2BrCwQNPjaDJzdZAaOgCkm7ox7%2BXY60A%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Gergely Polonkai

unread,
Nov 3, 2015, 1:58:42 AM11/3/15
to Django users


On 3 Nov 2015 06:07, "Mike Dewhirst" <mi...@dewhirst.com.au> wrote:
>
> On 3/11/2015 9:55 AM, Alex Heyden wrote:
>>
>> I tried putting the model back in its original module with some much smaller changes, but I'm getting the same error. The error is nonsense in the current context.
>>
>> Is there some intermediate state saved somewhere when you try to run makemigrations?
>
>
> Not as far as I can tell. I too have had difficulty moving models between apps. It is in my too-hard basket for the moment. However ...
>
> When I try again I'll create the model-to-be-moved in its new app and get that working properly. I would definitely do a makemigration but edit the migration file prior to migrating to use a forwards_func() method to get the data across and entrenched in its proper relations with other models on creation. Finally, when all is working I'd bravely drop the old model in a separate migration.

I totally agree with this approach. You copy the model from app A to app B, update all objects to use app B and copy all data from A to B with a RunPython function. This all should go in a migration file under app B. Then, from within a migration under app A, remove the model. If you are sure this app won't ever be used outside your project, you can make the migration in app A to depend on the migration in app B, either, so there will be no chance that your data gets copied before it is deleted.

Best,
Gergely

> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.

> To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/563840E7.7060103%40dewhirst.com.au.

Reply all
Reply to author
Forward
0 new messages