Django unittest - 无法创建表

时间:2015-06-14 10:50:41

标签: mysql django unit-testing

我正在尝试为我的django应用程序编写简单的测试。但每次运行./manage.py test时都会失败。

Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-package/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 30, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 74, in execute
super(Command, self).execute(*args, **options)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 90, in handle
failures = test_runner.run_tests(test_labels)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/test/runner.py", line 210, in run_tests
old_config = self.setup_databases()
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/test/runner.py", line 166, in setup_databases
**kwargs
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/test/runner.py", line 370, in setup_databases
serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/db/backends/base/creation.py", line 368, in create_test_db
test_flush=not keepdb,
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 120, in call_command
return command.execute(*args, **defaults)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 179, in handle
created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 317, in sync_apps
cursor.execute(statement)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 124, in execute
return self.cursor.execute(query, args)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/home/tyler/projects/django/env/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.OperationalError: (1005, "Can't create table 'test_products.#sql-4b8_e9' (errno: 150)")

我认为问题出在MySQL引擎上,但我在MySQL工作台上检查过它是InnoDb引擎。也许有人有同样的问题。

1 个答案:

答案 0 :(得分:3)

在我从Django 1.5移植到Django 1.8的django应用程序上运行manage.py test时,我遇到了完全相同的问题。

./manage.py test
Creating test database for alias 'default'...
Traceback (most recent call last):
  File "./manage.py", line 10, in <module>

...

  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 'test_xxxx.#sql-6c1_6c' (errno: 150)")

正如Sergii V.所说,解决方案与迁移有关。在我的情况下,根本没有迁移文件(该功能在Django 1.5中不存在)。

所以解决方案正在运行

./manage.py makemigrations <appname>

之后,测试工作正常:

./manage.py test
Creating test database for alias 'default'...
.
----------------------------------------------------------------------
Ran 1 test in 0.014s

OK
Destroying test database for alias 'default'...