Django测试运行器导致数据库操作错误

时间:2016-12-19 11:08:57

标签: python mysql django testing

我继承了一个使用Python 3.4构建的网站,Django 1.10带有一个MySQL数据库,除了一些旧的,未使用的遗留测试之外,它没有进行任何测试。我已经开始编写测试但在尝试运行manage.py test时遇到错误(在引发异常之前删除旧数据库大约需要5分钟)

Creating test database for alias 'default'... Got an error creating
the test database: (1007, "Can't create database 'test_rmpublic';
database exists") Type 'yes' if you would like to try deleting the
test database 'test_rmpublic', or 'no' to cancel: yes Destroying old
test database for alias 'default'... Traceback (most recent call
last):   File
"/projectpath/lib/python3.4/site-packages/django/db/backends/utils.py",
line 64, in execute
    return self.cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/mysql/base.py",
line 110, in execute
    return self.cursor.execute(query, args)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
220, in execute
    self.errorhandler(self, exc, value)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/connections.py",
line 36, in defaulterrorhandler
    raise errorvalue   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
209, in execute
    r = self._query(query)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
371, in _query
    rowcount = self._do_query(q)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
335, in _do_query
    db.query(q)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/connections.py",
line 280, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1072, "Key column 'establishment_id' doesn't exist in table")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):   File "manage.py", line 10, in
<module>
    execute_from_command_line(sys.argv)   File "/projectpath/lib/python3.4/site-packages/django/core/management/__init__.py",
line 367, in execute_from_command_line
    utility.execute()   File "/projectpath/lib/python3.4/site-packages/django/core/management/__init__.py",
line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)   File "/projectpath/lib/python3.4/site-packages/django/core/management/commands/test.py",
line 29, in run_from_argv
    super(Command, self).run_from_argv(argv)   File "/projectpath/lib/python3.4/site-packages/django/core/management/base.py",
line 294, in run_from_argv
    self.execute(*args, **cmd_options)   File "/projectpath/lib/python3.4/site-packages/django/core/management/base.py",
line 345, in execute
    output = self.handle(*args, **options)   File "/projectpath/lib/python3.4/site-packages/django/core/management/commands/test.py",
line 72, in handle
    failures = test_runner.run_tests(test_labels)   File "/projectpath/lib/python3.4/site-packages/django/test/runner.py", line
549, in run_tests
    old_config = self.setup_databases()   File "/projectpath/lib/python3.4/site-packages/django/test/runner.py", line
499, in setup_databases
    self.parallel, **kwargs   File "/projectpath/lib/python3.4/site-packages/django/test/runner.py", line
743, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),   File
"/projectpath/lib/python3.4/site-packages/django/db/backends/base/creation.py",
line 70, in create_test_db
    run_syncdb=True,   File "/projectpath/lib/python3.4/site-packages/django/core/management/__init__.py",
line 130, in call_command
    return command.execute(*args, **defaults)   File "/projectpath/lib/python3.4/site-packages/django/core/management/base.py",
line 345, in execute
    output = self.handle(*args, **options)   File "/projectpath/lib/python3.4/site-packages/django/core/management/commands/migrate.py",
line 204, in handle
    fake_initial=fake_initial,   File "/projectpath/lib/python3.4/site-packages/django/db/migrations/executor.py",
line 115, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)   File
"/projectpath/lib/python3.4/site-packages/django/db/migrations/executor.py",
line 145, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)   File
"/projectpath/lib/python3.4/site-packages/django/db/migrations/executor.py",
line 244, in apply_migration
    state = migration.apply(state, schema_editor)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/base/schema.py",
line 92, in __exit__
    self.execute(sql)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/base/schema.py",
line 112, in execute
    cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/utils.py",
line 64, in execute
    return self.cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/utils.py", line
94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)   File "/projectpath/lib/python3.4/site-packages/django/utils/six.py", line
685, in reraise
    raise value.with_traceback(tb)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/utils.py",
line 64, in execute
    return self.cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/mysql/base.py",
line 110, in execute
    return self.cursor.execute(query, args)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
220, in execute
    self.errorhandler(self, exc, value)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/connections.py",
line 36, in defaulterrorhandler
    raise errorvalue   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
209, in execute
    r = self._query(query)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
371, in _query
    rowcount = self._do_query(q)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/cursors.py", line
335, in _do_query
    db.query(q)   File "/projectpath/lib/python3.4/site-packages/MySQLdb/connections.py",
line 280, in query
    _mysql.connection.query(self, query) django.db.utils.OperationalError: (1072, "Key column
'establishment_id' doesn't exist in table")

我查看了所有迁移文件,但看不到对establish_id的任何引用。认为它可能是旧的test_db的一个问题,我使用this问题中描述的hacky但接受的答案来使用sqlite3作为测试数据库,并得到一个新的错误:

Traceback (most recent call last):   File
"/projectpath/lib/python3.4/site-packages/django/db/backends/utils.py",
line 64, in execute
    return self.cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py",
line 337, in execute
    return Database.Cursor.execute(self, query, params) sqlite3.OperationalError: no such table:
rmcustomer_establishmentradius

The above exception was the direct cause of the following exception:

Traceback (most recent call last):   File "manage.py", line 10, in
<module>
    execute_from_command_line(sys.argv)   File "/projectpath/lib/python3.4/site-packages/django/core/management/__init__.py",
line 367, in execute_from_command_line
    utility.execute()   File "/projectpath/lib/python3.4/site-packages/django/core/management/__init__.py",
line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)   File "/projectpath/lib/python3.4/site-packages/django/core/management/commands/test.py",
line 29, in run_from_argv
    super(Command, self).run_from_argv(argv)   File "/projectpath/lib/python3.4/site-packages/django/core/management/base.py",
line 294, in run_from_argv
    self.execute(*args, **cmd_options)   File "/projectpath/lib/python3.4/site-packages/django/core/management/base.py",
line 345, in execute
    output = self.handle(*args, **options)   File "/projectpath/lib/python3.4/site-packages/django/core/management/commands/test.py",
line 72, in handle
    failures = test_runner.run_tests(test_labels)   File "/projectpath/lib/python3.4/site-packages/django/test/runner.py", line
549, in run_tests
    old_config = self.setup_databases()   File "/projectpath/lib/python3.4/site-packages/django/test/runner.py", line
499, in setup_databases
    self.parallel, **kwargs   File "/projectpath/lib/python3.4/site-packages/django/test/runner.py", line
743, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),   File
"/projectpath/lib/python3.4/site-packages/django/db/backends/base/creation.py",
line 78, in create_test_db
    self.connection._test_serialized_contents = self.serialize_db_to_string()   File
"/projectpath/lib/python3.4/site-packages/django/db/backends/base/creation.py",
line 122, in serialize_db_to_string
    serializers.serialize("json", get_objects(), indent=None, stream=out)   File
"/projectpath/lib/python3.4/site-packages/django/core/serializers/__init__.py",
line 129, in serialize
    s.serialize(queryset, **options)   File "/projectpath/lib/python3.4/site-packages/django/core/serializers/base.py",
line 79, in serialize
    for count, obj in enumerate(queryset, start=1):   File "/projectpath/lib/python3.4/site-packages/django/db/backends/base/creation.py",
line 118, in get_objects
    for obj in queryset.iterator():   File "/projectpath/lib/python3.4/site-packages/django/db/models/query.py",
line 54, in __iter__
    results = compiler.execute_sql()   File "/projectpath/lib/python3.4/site-packages/django/db/models/sql/compiler.py",
line 835, in execute_sql
    cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/utils.py",
line 64, in execute
    return self.cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/utils.py", line
94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)   File "/projectpath/lib/python3.4/site-packages/django/utils/six.py", line
685, in reraise
    raise value.with_traceback(tb)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/utils.py",
line 64, in execute
    return self.cursor.execute(sql, params)   File "/projectpath/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py",
line 337, in execute
    return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: no such table:
rmcustomer_establishmentradius

为臃肿的堆栈痕迹道歉。任何帮助都会很棒。

0 个答案:

没有答案