繁体   English   中英

单元测试django应用程序错误创建数据库表OperationalError:(1005,“无法创建表...”)

[英]Unit testing django application error creating database table OperationalError: (1005, "Can't create table…)

我试图在我的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的类。

我有相同的(非常一般)错误消息。 对我来说,它归结为忘记在新的应用程序上运行makemigrations。 也可以看看:

https://docs.djangoproject.com/en/1.8/topics/migrations/#dependencies

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM