Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#21872 closed Bug (fixed)

With custom User, test database is not created due to missing auth.Group relation

Reported by: Max Arnold Owned by: Andrew Godwin <andrew@…>
Component: Migrations Version: 1.7-alpha-1
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

As described here: https://github.com/django/django/commit/941d23e54890bcc9e73734c5c1f3c82193fd97d6

I have custom User model and it worked fine at least on 1.5. On 1.7a1 my application works too, but I can't run any tests - database is not created due to missing auth.Group relation ("ValueError: Related model 'auth.Group' cannot be resolved").

Below are traceback

Traceback (most recent call last):
  File "src/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/core/management/__init__.py", line 427, in execute_from_command_line
    utility.execute()
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/core/management/__init__.py", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/core/management/base.py", line 337, in execute
    output = self.handle(*args, **options)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/test/runner.py", line 147, in run_tests
    old_config = self.setup_databases()
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/test/runner.py", line 109, in setup_databases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/test/runner.py", line 299, in setup_databases
    verbosity, autoclobber=not interactive)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/db/backends/creation.py", line 367, in create_test_db
    test_database=True)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/core/management/__init__.py", line 167, in call_command
    return klass.execute(*args, **defaults)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/core/management/base.py", line 337, in execute
    output = self.handle(*args, **options)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 145, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/db/migrations/executor.py", line 60, in migrate
    self.apply_migration(migration, fake=fake)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/db/migrations/executor.py", line 94, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/db/migrations/migration.py", line 97, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 28, in database_forwards
    schema_editor.create_model(model)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/db/backends/schema.py", line 256, in create_model
    self.create_model(field.rel.through)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/db/backends/schema.py", line 182, in create_model
    definition, extra_params = self.column_sql(model, field)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/db/backends/schema.py", line 109, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1707, in db_parameters
    return {"type": self.db_type(connection), "check": []}
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1698, in db_type
    rel_field = self.related_field
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1605, in related_field
    return self.foreign_related_fields[0]
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1367, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields)
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1354, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "/Users/user/.virtualenvs/prj/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1339, in resolve_related_fields
    raise ValueError('Related model %r cannot be resolved' % self.rel.to)
ValueError: Related model 'auth.Group' cannot be resolved

and debug context

   1339         if isinstance(self.rel.to, six.string_types):
-> 1340             raise ValueError('Related model %r cannot be resolved' % self.rel.to)
   1341         related_fields = []

ipdb> self.rel.to
'auth.Group'
ipdb> six.string_types
(<type 'basestring'>,)
ipdb> self.model
<class '__fake__.User_groups'>

Test machine: OS X Mavericks, Python 2.7.6. Django 1.7-alpha-1 (also reproducible on master revision 29ddae7436e8). Sample code attached.

Attachments (1)

example.tar.bz2 (3.8 KB ) - added by Max Arnold 11 years ago.
Minimal project to reproduce the problem (run ./manage.py test myauth)

Download all attachments as: .zip

Change History (6)

by Max Arnold, 11 years ago

Attachment: example.tar.bz2 added

Minimal project to reproduce the problem (run ./manage.py test myauth)

comment:1 by Baptiste Mispelon, 11 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

Hi,

I can reproduce this issue with this simple model:

from django.db import models

from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin

class User(AbstractBaseUser, PermissionsMixin):
    USERNAME_FIELD = 'id'

With this model (and changing the settings to point AUTH_USER_MODEL to it), doing the initial makemigrations works but migrate fails with the error. However, migrate will work if the migrations can be faked (for example if the table has been created using a different version of Django).
The error can always be triggered by doing ./manage.py test <appname> (where <appname> is the name of the app containing the custom user model).

I'm marking this as a release blocker as well since things work on Django 1.6.

Thanks.

comment:2 by Andrew Godwin <andrew@…>, 11 years ago

Owner: set to Andrew Godwin <andrew@…>
Resolution: fixed
Status: newclosed

In 6758a9c0fc34dc9944895b98045d2b26c8147ea2:

Fixed #21872: Not detecting dependencies from M2M fields

comment:3 by Max Arnold, 11 years ago

Resolution: fixed
Status: closednew

With this fix I'm still getting the same error using example project.

comment:4 by Andrew Godwin, 11 years ago

Resolution: fixed
Status: newclosed

The error is in the generated migration - you'll need to delete that and re-create it to fix.

comment:5 by Max Arnold, 11 years ago

Thanks for the fix!

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