Django 1.7 dev: migration fails when adding a m2m (Related model cannot be resolved)

217 views
Skip to first unread message

baptist...@gmail.com

unread,
Mar 5, 2014, 4:57:27 AM3/5/14
to django...@googlegroups.com
Hello,

I'm struggling with migrations when trying to add a m2m between two apps in Django dev. I don't know if this is a bug or if I'm doing it wrong but basically, if main.models is:

from django.conf import settings
from django.db import models

class Restaurant(models.Model):
    name = models.CharField(max_length = 80)
   
class UserRestaurant(models.Model):
    restaurant = models.ForeignKey(Restaurant)
    user = models.ForeignKey('custom_auth.User')


And custom_auth.models is:

from django.db import models

class User(models.Model):
    address = models.CharField(max_length=100, blank=True)


Then adding the following field to custom_auth.models.User (that cannot be done in one migration because of a CircularDependency exception):

    restaurants = models.ManyToManyField('main.Restaurant')

 Will raise the following exception when migrating (makemigrations is fine):

$ python3 manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: admin, sessions, auth, contenttypes
  Apply all migrations: custom_auth, main
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations:
  Applying custom_auth.0002_user_restaurants...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/core/management/__init__.py", line 427, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/core/management/__init__.py", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/core/management/base.py", line 287, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/core/management/base.py", line 336, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/core/management/commands/migrate.py", line 145, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/db/migrations/executor.py", line 60, in migrate
    self.apply_migration(migration, fake=fake)
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/db/migrations/executor.py", line 94, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/db/migrations/migration.py", line 97, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/db/migrations/operations/fields.py", line 35, in database_forwards
    field,
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/db/backends/sqlite3/schema.py", line 110, in add_field
    return self.create_model(field.rel.through)
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/db/backends/schema.py", line 191, in create_model
    definition, extra_params = self.column_sql(model, field)
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/db/backends/schema.py", line 108, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/db/models/fields/related.py", line 1754, in db_parameters
    return {"type": self.db_type(connection), "check": []}
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/db/models/fields/related.py", line 1745, in db_type
    rel_field = self.related_field
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/db/models/fields/related.py", line 1651, in related_field
    return self.foreign_related_fields[0]
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/db/models/fields/related.py", line 1410, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields)
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/db/models/fields/related.py", line 1397, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "/usr/local/lib/python3.3/dist-packages/Django-1.7a2-py3.3.egg/django/db/models/fields/related.py", line 1382, in resolve_related_fields
    raise ValueError('Related model %r cannot be resolved' % self.rel.to)
ValueError: Related model 'main.Restaurant' cannot be resolved

 
I'm attaching this test project to the email, you'll see that the last migration will fail.

Am I missing something obvious here? The FK in main.UserRestaurant is working fine but I don't know what can't Django resolve main.Restaurant in custom_auth.User...

Thank you for your help
Baptiste
test_django.tar.gz
Reply all
Reply to author
Forward
0 new messages