Opened 4 years ago

Closed 3 years ago

#22527 closed Bug (fixed)

FK proxy model not properly deferred in syncdb

Reported by: leftmoose Owned by: nobody
Component: Database layer (models, ORM) Version: 1.6
Severity: Normal Keywords: proxy model, constraints
Cc: Tim Graham Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no



installed_apps = (
    app1, app2, app3

db: postgres (to enfoce constraints)

from django.db import models

class Model1(models.Model):
    foreign2 = models.ForeignKey('app2.Model2')

from app3.models import Model3

class Model2(Model3):
    class Meta:
        proxy = True

from django.db import models

# Create your models here.
class Model3(models.Model):
$ ./ test -v2 app1

Creating test database for alias 'default' ('test_proxy_model_bug')...
Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_groups
Creating table auth_user_user_permissions
Creating table auth_user
Creating table django_content_type
Creating table django_session
Creating table app1_model1
Creating table app3_model3
Traceback (most recent call last):
  File "./", line 10, in <module>
  File "/Users/david/dev/django_source/django/core/management/", line 397, in execute_from_command_line
  File "/Users/david/dev/django_source/django/core/management/", line 390, in execute
  File "/Users/david/dev/django_source/django/core/management/commands/", line 51, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/Users/david/dev/django_source/django/core/management/", line 240, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Users/david/dev/django_source/django/core/management/commands/", line 72, in execute
    super(Command, self).execute(*args, **options)
  File "/Users/david/dev/django_source/django/core/management/", line 283, in execute
    output = self.handle(*args, **options)
  File "/Users/david/dev/django_source/django/core/management/commands/", line 89, in handle
    failures = test_runner.run_tests(test_labels)
  File "/Users/david/dev/django_source/django/test/", line 145, in run_tests
    old_config = self.setup_databases()
  File "/Users/david/dev/django_source/django/test/", line 107, in setup_databases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File "/Users/david/dev/django_source/django/test/", line 279, in setup_databases
    verbosity, autoclobber=not interactive)
  File "/Users/david/dev/django_source/django/db/backends/", line 339, in create_test_db
  File "/Users/david/dev/django_source/django/core/management/", line 159, in call_command
    return klass.execute(*args, **defaults)
  File "/Users/david/dev/django_source/django/core/management/", line 283, in execute
    output = self.handle(*args, **options)
  File "/Users/david/dev/django_source/django/core/management/", line 413, in handle
    return self.handle_noargs(**options)
  File "/Users/david/dev/django_source/django/core/management/commands/", line 107, in handle_noargs
  File "/Users/david/dev/django_source/django/db/", line 105, in inner
    return func(*args, **kwargs)
  File "/Users/david/dev/django_source/django/db/", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/david/dev/django_source/django/db/", line 105, in inner
    return func(*args, **kwargs)
django.db.utils.ProgrammingError: relation "app3_model3" does not exist

git bisect reveals that this started happening in

196cc875b26f266e8f8dfd5be9daa0b8f246b9cd is the first bad commit
commit 196cc875b26f266e8f8dfd5be9daa0b8f246b9cd
Author: Tim Graham <>
Date:   Thu Aug 1 14:09:47 2013 -0400

    [1.6.x] Fixed #17519 -- Fixed missing SQL constraints to proxy models.

    Thanks thibaultj for the report, jenh for the patch,
    and charettes for the tests.

    Backport of aa830009de from master

so i guess this may be considered an improvement :p

from some pdb digging, it looks like the issue is that once the proxy model Model2 is created (but before the concrete one Model3 is), syncdb tries to setup all deferred constraints, though in this case, the dependency is on Model3, not Model2.

Change History (2)

comment:1 Changed 4 years ago by Tim Graham

Cc: Tim Graham added
Component: UncategorizedDatabase layer (models, ORM)
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 Changed 3 years ago by Tim Graham

Resolution: fixed
Status: newclosed

This doesn't appear to be an issue when using migrations.

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