我试图在我的django应用程序中将我的模型联合起来,这些应用程序包含一些迁移。我准备好了我的测试,但是在运行时
./manage.py test my_app
我收到以下错误
django.db.utils.OperationalError: (1005, "Can't create table 'test_rhombusdb.#sql-4ca_2c' (errno: 150)")
数据库是正常创建的,所以我想我有权创建数据库。 我真的不知道这是哪一张表。迁移通过正常,db在生产中正常工作。这可能是什么问题?
你想要什么其他信息?
我的堆栈跟踪
File "./manage.py", line 18, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/test.py", line 30, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/test.py", line 74, in execute
super(Command, self).execute(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/test.py", line 90, in handle
failures = test_runner.run_tests(test_labels)
File "/usr/local/lib/python2.7/dist-packages/django/test/runner.py", line 210, in run_tests
old_config = self.setup_databases()
File "/usr/local/lib/python2.7/dist-packages/django/test/runner.py", line 166, in setup_databases
**kwargs
File "/usr/local/lib/python2.7/dist-packages/django/test/runner.py", line 370, in setup_databases
serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/creation.py", line 368, in create_test_db
test_flush=not keepdb,
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 120, in call_command
return command.execute(*args, **defaults)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 444, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 179, in handle
created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 317, in sync_apps
cursor.execute(statement)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 124, in execute
return self.cursor.execute(query, args)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.OperationalError: (1005, "Can't create table 'testdb.#sql-4ca_7c' (errno: 150)")
我可以看到调用迁移时出错。
PS:我下载了我的数据库(真正的一个,但开发一个),并尝试重新创建运行迁移的表。我带来了同样的错误。再次运行迁移工作就像一个魅力....这里真的非常错误....
PS2:在没有迁移文件夹的情况下运行迁移后删除所有迁移文件夹并启动时没有给我同样的错误....非常糟糕的错误很难调试
PS3:我的模型中的外键存在问题(至少到目前为止我测试过)。我尝试停用应用程序并按照条款激活它们,当我到达特定的应用程序时,发生错误。我隔离了一个模型,并给了我这个问题。我注释掉外键并且工作了。再次取消注释其中一个错误。class Patient(models.Model):
#many fields here
doctor = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="patients")
它与1.6和sqlite3一起工作,我移动到1.8和mysql,并且繁荣......错误。
AUTH_USER_MODEL = 'users.MyCustomUser'
MyCustomUser是一个继承AbstractBaseUser和PermissionsMixin的类。
答案 0 :(得分:1)
我有相同的(非常一般的)错误消息。对我来说,它归结为忘记在新的应用程序上运行makemigrations。另见:
https://docs.djangoproject.com/en/1.8/topics/migrations/#dependencies