django - Phantom ForeignKey to similar model representing a DB view -
i'm trying 'phantom' foreignkey reference model view on backend. view assembles data includes data originating table(so share primary key).
here's simplified version of we've had setup(view more complex model, chosen design way).
class vspouse(models.model): person_id = models.integerfield(primary_key=true) ... class person(models.model): person_id = models.autofield(primary_key=true) spouse = models.foreignkey(vspouse, db_column = 'person_id', to_field='person_id', null=true) ...
now, since tables designed on backend before models, have never used syncdb
. because of that, we've never noticed problem , things work expected.
however, we're starting develop django tests, , when begins build test db, see following:
$ python2 manage.py test misc --traceback creating test database alias 'default'... destroying old test database 'default'... traceback (most recent call last): file "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 222, in run_from_argv self.execute(*args, **options.__dict__) file "/usr/lib/python2.7/site-packages/django/core/management/commands/test.py", line 72, in execute super(command, self).execute(*args, **options) file "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 255, in execute output = self.handle(*args, **options) file "/usr/lib/python2.7/site-packages/django/core/management/commands/test.py", line 89, in handle failures = test_runner.run_tests(test_labels) file "/usr/lib/python2.7/site-packages/django/test/simple.py", line 367, in run_tests old_config = self.setup_databases() file "/usr/lib/python2.7/site-packages/django/test/simple.py", line 315, in setup_databases self.verbosity, autoclobber=not self.interactive) file "/usr/lib/python2.7/site-packages/django/db/backends/creation.py", line 293, in create_test_db load_initial_data=false) file "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 161, in call_command return klass.execute(*args, **defaults) file "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 255, in execute output = self.handle(*args, **options) file "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 385, in handle return self.handle_noargs(**options) file "/usr/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 102, in handle_noargs cursor.execute(statement) file "/usr/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 58, in execute six.reraise(utils.databaseerror, utils.databaseerror(*tuple(e.args)), sys.exc_info()[2]) file "/usr/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 54, in execute return self.cursor.execute(query, args) databaseerror: column "person_id" specified more once
now, guess question is, there 'proper' ways of doing or working around problem? great if add switch real=false
field definition wouldn't try , generate column when building test db. however, don't think that's possible.
actually, can. it's not called real
, abstract
, , applies models (but should able use inheritance address this).
your model structure might need little massaging correspond django expects, should started in right direction.
it's documented here.
Comments
Post a Comment