﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
22527	FK proxy model not properly deferred in syncdb	leftmoose	nobody	"Setup:

{{{
#!python
installed_apps = (
    ...
    app1, app2, app3
)

db: postgres (to enfoce constraints)

app1.models:
from django.db import models

class Model1(models.Model):
    foreign2 = models.ForeignKey('app2.Model2')

app2.models:
from app3.models import Model3

class Model2(Model3):
    class Meta:
        proxy = True

app3.models:
from django.db import models

# Create your models here.
class Model3(models.Model):
    pass
}}}

{{{
#!bash
$ ./manage.py 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 ""./manage.py"", line 10, in <module>
    execute_from_command_line(sys.argv)
  File ""/Users/david/dev/django_source/django/core/management/__init__.py"", line 397, in execute_from_command_line
    utility.execute()
  File ""/Users/david/dev/django_source/django/core/management/__init__.py"", line 390, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ""/Users/david/dev/django_source/django/core/management/commands/test.py"", line 51, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File ""/Users/david/dev/django_source/django/core/management/base.py"", line 240, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ""/Users/david/dev/django_source/django/core/management/commands/test.py"", line 72, in execute
    super(Command, self).execute(*args, **options)
  File ""/Users/david/dev/django_source/django/core/management/base.py"", line 283, in execute
    output = self.handle(*args, **options)
  File ""/Users/david/dev/django_source/django/core/management/commands/test.py"", line 89, in handle
    failures = test_runner.run_tests(test_labels)
  File ""/Users/david/dev/django_source/django/test/runner.py"", line 145, in run_tests
    old_config = self.setup_databases()
  File ""/Users/david/dev/django_source/django/test/runner.py"", line 107, in setup_databases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File ""/Users/david/dev/django_source/django/test/runner.py"", line 279, in setup_databases
    verbosity, autoclobber=not interactive)
  File ""/Users/david/dev/django_source/django/db/backends/creation.py"", line 339, in create_test_db
    load_initial_data=False)
  File ""/Users/david/dev/django_source/django/core/management/__init__.py"", line 159, in call_command
    return klass.execute(*args, **defaults)
  File ""/Users/david/dev/django_source/django/core/management/base.py"", line 283, in execute
    output = self.handle(*args, **options)
  File ""/Users/david/dev/django_source/django/core/management/base.py"", line 413, in handle
    return self.handle_noargs(**options)
  File ""/Users/david/dev/django_source/django/core/management/commands/syncdb.py"", line 107, in handle_noargs
    cursor.execute(statement)
  File ""/Users/david/dev/django_source/django/db/utils.py"", line 105, in inner
    return func(*args, **kwargs)
  File ""/Users/david/dev/django_source/django/db/utils.py"", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File ""/Users/david/dev/django_source/django/db/utils.py"", 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 <timograham@gmail.com>
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`.

"	Bug	closed	Database layer (models, ORM)	1.6	Normal	fixed	proxy model, constraints	Tim Graham	Accepted	0	0	0	0	0	0
