我正在使用多个数据库(同一个postgresql数据库中的不同模式)处理Django 1.9和python 3.3项目 . 当我第一次尝试迁移项目时,我收到了这个错误
Running migrations:
Rendering model states... DONE
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying MyApp.0001_initial...Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: ERROR: relation "auth_user" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 10, in
execute_from_command_line(sys.argv)
File "/usr/lib/python3.4/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute()
File "/usr/lib/python3.4/site-packages/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/lib/python3.4/site-packages/django/core/management/base.py", line 399, in execute
output = self.handle(*args, **options)
File "/usr/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "/usr/lib/python3.4/site-packages/django/db/migrations/executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/lib/python3.4/site-packages/django/db/migrations/executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/usr/lib/python3.4/site-packages/django/db/migrations/executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "/usr/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 90, in __exit__
self.execute(sql)
File "/usr/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 110, in execute
cursor.execute(sql, params)
File "/usr/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python3.4/site-packages/django/db/utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/lib/python3.4/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/usr/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: ERROR: relation "auth_user" does not exist
当未迁移 auth_user
表时,此错误似乎出现在其他项目中 . 在我的情况下,在迁移需要它的应用程序之前开始使用 manage.py migrate auth
进行迁移并不能解决问题 .
我怀疑问题来自于在Django中使用不同的数据库 . 我的 auth_user
表存储在 default database 中,models.py的内容被路由到 other database .
迁移过程是否在与我的应用程序数据库相同的数据库中查找 auth_user
表?这是完全不同的东西吗?
1 回答
回答问题所在
它实际上是一个跨数据库引用问题 . Django无法创建跨数据库外键 .
来自Django 1.8 documentation(以下版本没有任何解决方案(当前版本为1.10)):
如何解决方法并保留单独的数据库
在我的情况下,因为路由器正在工作,所以有一点hack来简化跨数据库对象 .
有了这个,我可以使用方法
set_user
和get_user
来处理存储在我的对象MyClassWithCrossDB
中的用户 .当然它's not perfect because it doesn'允许自动操作,如
on_delete=models.CASCADE
,我不得不使用方法而不是实例变量 . 但它是解决方法的解决方案 .