我有一个django网站使用最新版本的django(截至目前为1.7.4)。当我运行我的测试时,它们都会失败,因为django抱怨我的一个模型在测试数据库中不存在。错误如下:
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Library/Python/2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
super(Command, self).run_from_argv(argv)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Library/Python/2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
super(Command, self).execute(*args, **options)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/Library/Python/2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
failures = test_runner.run_tests(test_labels)
File "/Library/Python/2.7/site-packages/django/test/runner.py", line 147, in run_tests
old_config = self.setup_databases()
File "/Library/Python/2.7/site-packages/django/test/runner.py", line 109, in setup_databases
return setup_databases(self.verbosity, self.interactive, **kwargs)
File "/Library/Python/2.7/site-packages/django/test/runner.py", line 299, in setup_databases
serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
File "/Library/Python/2.7/site-packages/django/db/backends/creation.py", line 385, in create_test_db
self.connection._test_serialized_contents = self.serialize_db_to_string()
File "/Library/Python/2.7/site-packages/django/db/backends/creation.py", line 421, in serialize_db_to_string
serializers.serialize("json", get_objects(), indent=None, stream=out)
File "/Library/Python/2.7/site-packages/django/core/serializers/__init__.py", line 128, in serialize
s.serialize(queryset, **options)
File "/Library/Python/2.7/site-packages/django/core/serializers/base.py", line 52, in serialize
for obj in queryset:
File "/Library/Python/2.7/site-packages/django/db/backends/creation.py", line 417, in get_objects
for obj in queryset.iterator():
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 265, in iterator
for row in compiler.results_iter():
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 700, in results_iter
for rows in self.execute_sql(MULTI):
File "/Library/Python/2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 129, in execute
return self.cursor.execute(query, args)
File "/Library/Python/2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
django.db.utils.ProgrammingError: (1146, "Table 'test_app.user_detail' doesn't exist")
奇怪的是我的模型被定义为:
class UserDetail(models.Model):
class Meta:
db_table = 'user_detail'
user = models.ForeignKey("auth.User")
创建了settings.py
中的所有其他模型(来自所有应用)。出于某种原因,这个应用程序是测试数据库中不存在该模型的唯一应用程序。还有什么我在这里做错了吗?为什么没有创建这一个表?
修改
结果我必须将managed = False
放在Meta
。