Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 单元测试django应用程序错误创建数据库表操作错误:(1005,“Can';t创建表…”_Python_Django_Django Unittest - Fatal编程技术网

Python 单元测试django应用程序错误创建数据库表操作错误:(1005,“Can';t创建表…”

Python 单元测试django应用程序错误创建数据库表操作错误:(1005,“Can';t创建表…”,python,django,django-unittest,Python,Django,Django Unittest,我试图在django应用程序中统一测试我的模型,该应用程序包含一些迁移。我已经准备好了测试,但是在运行时 ./manage.py test my_app 我得到以下错误 django.db.utils.OperationalError: (1005, "Can't create table 'test_rhombusdb.#sql-4ca_2c' (errno: 150)") 数据库是正常创建的,所以我想我有创建数据库的权限。 我真的不知道这是指哪张桌子。迁移正常,db在生产中工作良好。这里

我试图在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)")
它与1.6和sqlite3一起工作,我转到了1.8和mysql,然后砰的一声……错误

AUTH_USER_MODEL = 'users.MyCustomUser'
MyCustomUser是继承AbstractBaseUser和PermissionsMixin的类

我收到了相同的(相当普遍的)错误消息。对我来说,这归结为忘记在新应用程序上运行makemigrations。另见:


尝试删除所有表(测试数据库),然后再次运行测试,请参见:,这也可能是订单迁移问题。我使用的是django 1.8.x。我使用的是MySql,不存在测试数据库。当它抛出异常时,数据库不会被删除,所以它会留在我的服务器上。如果我重新运行测试,它会要求我删除它。删除它将导致相同的问题,可能是因为它无法创建数据库。我认为这个错误是指你链接中的syncdb,而我的错误发生在测试过程中。当我运行migrate(相当于syncdb)时,一切正常。我已经从1.6迁移到1.8,并使用--false inital flag创建了迁移。很高兴它能工作,是的,现在使用migrate命令是正确的方法。它不起作用……我的问题是在测试时。/migrate test给了我错误对不起,我理解错了,那么您的db用户有正确的权限(创建、删除dbs)吗?
AUTH_USER_MODEL = 'users.MyCustomUser'