Opened 12 months ago

Closed 12 months ago

Last modified 12 months ago

#22332 closed Uncategorized (duplicate)

intra-app dependencies from models with circular references cause exceptions on database creation

Reported by: bernie_sumption Owned by: nobody
Component: Migrations Version: 1.7-beta-1
Severity: Normal Keywords:
Cc: loic@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Here is some minimal sample code that triggers this bug. Create a new project and a new app called "bug", then add this code to bug/models.py:

from django.db import models

class A(models.Model):
    intra_app_fk = models.ForeignKey("contenttypes.ContentType")
    circular_fk = models.ForeignKey("bug.A")

Run python manage.py makemigrations and python manage.py test, and it will fail with the stack trace below.

Notes:

  • there's nothing special about contenttypes.ContentType. If you create a second app in the same project with a model called "B" and try to create a FK relationship to B, it will fail with the same error
  • It does however have to be an intra-app relationship. Creating a model "B" in the same models.py does not trigger the bug
  • circular_fk does not have to be a self-reference. It can be a circular reference that passes through another model

Full stack trace:

Creating test database for alias 'default'...
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/core/management/__init__.py", line 427, in execute_from_command_line
    utility.execute()
  File "/Users/bernie/.virtualenvs/mgerp/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/bernie/.virtualenvs/mgerp/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/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/core/management/base.py", line 337, in execute
    output = self.handle(*args, **options)
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/test/runner.py", line 147, in run_tests
    old_config = self.setup_databases()
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/test/runner.py", line 109, in setup_databases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/test/runner.py", line 297, in setup_databases
    verbosity, autoclobber=not interactive)
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/db/backends/creation.py", line 368, in create_test_db
    test_database=True)
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/core/management/__init__.py", line 167, in call_command
    return klass.execute(*args, **defaults)
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/core/management/base.py", line 337, in execute
    output = self.handle(*args, **options)
  File "/Users/bernie/.virtualenvs/mgerp/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/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/db/migrations/executor.py", line 60, in migrate
    self.apply_migration(migration, fake=fake)
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/db/migrations/executor.py", line 94, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/Users/bernie/.virtualenvs/mgerp/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/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 28, in database_forwards
    schema_editor.create_model(model)
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/db/backends/schema.py", line 191, in create_model
    definition, extra_params = self.column_sql(model, field)
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/db/backends/schema.py", line 108, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1758, in db_parameters
    return {"type": self.db_type(connection), "check": []}
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1749, in db_type
    rel_field = self.related_field
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1655, in related_field
    return self.foreign_related_fields[0]
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1414, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields)
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1401, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "/Users/bernie/.virtualenvs/mgerp/lib/python2.7/site-packages/django/db/models/fields/related.py", line 1386, in resolve_related_fields
    raise ValueError('Related model %r cannot be resolved' % self.rel.to)
ValueError: Related model 'contenttypes.ContentType' cannot be resolved

Change History (4)

comment:1 Changed 12 months ago by bmispelon

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

This looks like a duplicate of #22319.

comment:2 Changed 12 months ago by loic84

  • Cc loic@… added

@bernie_sumption, any chance you could check if this patch solves your issue?

https://github.com/loic/django/compare/ticket22332

comment:3 Changed 12 months ago by bernie_sumption

@loic84 yes, this bug no longer happens when I install from the github trunk

Thanks!

comment:4 Changed 12 months ago by loic84

@bernie_sumption, do you mean the normal django trunk (https://github.com/django/django), or from my branch (https://github.com/loic/django/compare/ticket22332)?

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