【问题标题】:How to view the SQL created for test models in Django?如何在 Django 中查看为测试模型创建的 SQL?
【发布时间】:2016-01-08 18:29:09
【问题描述】:

在当前的 Django 项目中,我每次运行测试时都会得到一个django.db.utils.ProgrammingError——但前提是我使用 MySQL 后端(使用 mysqlclient for Python 3);如果我使用 SQLite 后端,测试运行良好:

django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'None, None) NOT NULL)' at line 1")

完整的追溯见下文。

尽管测试失败,但项目(网站和管理员)在使用内置服务器或 WSGI 应用程序时运行良好——同时具有两个后端。该错误仅在运行测试时发生。

我在 core 包中有一个 models.py,它定义了其他应用程序使用的一些抽象模型和 mixin。该包中还有一个test.py,其中定义了一些模型,这些模型使用那些抽象类和mixin,所以我可以测试它们。我猜错误就在那里……某处;但我无法从异常中弄清楚。

由于“真实”模型(不是抽象模型)仅在 test.py 中而不在 models.py 中,我不知道如何检查为它们创建的原始 SQL(使用django-admin sql 或类似)...

完整的回溯

$ django-admin test blog -v 3
Creating test database for alias 'default' ('yogalessontv_test')...
Got an error creating the test database: (1007, "Can't create database 'yogalessontv_test'; database exists")
Type 'yes' if you would like to try deleting the test database 'yogalessontv_test', or 'no' to cancel: yes
Destroying old test database 'default'...
Operations to perform:
  Synchronize unmigrated apps: streambox, profiles, core, suit, contact, allauth, messages, staticfiles, debug_toolbar
  Apply all migrations: admin, auth, carousel, sites, pages, socialaccount, blog, contenttypes, account, sessions, videos
Synchronizing apps without migrations:
Running pre-migrate handlers for application core
Running pre-migrate handlers for application pages
Running pre-migrate handlers for application blog
Running pre-migrate handlers for application videos
Running pre-migrate handlers for application carousel
Running pre-migrate handlers for application suit
Running pre-migrate handlers for application admin
Running pre-migrate handlers for application auth
Running pre-migrate handlers for application contenttypes
Running pre-migrate handlers for application sessions
Running pre-migrate handlers for application sites
Running pre-migrate handlers for application allauth
Running pre-migrate handlers for application account
Running pre-migrate handlers for application socialaccount
Running pre-migrate handlers for application debug_toolbar
  Creating tables...
    Creating table core_trimstringsmixintestmodel
    Creating table core_playtimemixintestmodel
Traceback (most recent call last):
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/MySQLdb/cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/MySQLdb/cursors.py", line 217, in execute
    res = self._query(query)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/MySQLdb/cursors.py", line 378, in _query
    rowcount = self._do_query(q)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/MySQLdb/cursors.py", line 341, in _do_query
    db.query(q)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'None, None) NOT NULL)' at line 1")

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

Traceback (most recent call last):
  File "/home/xxx/virtualenv/myproject/bin/django-admin", line 11, in <module>
    sys.exit(execute_from_command_line())
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/core/management/commands/test.py", line 30, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/core/management/commands/test.py", line 74, in execute
    super(Command, self).execute(*args, **options)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/core/management/commands/test.py", line 90, in handle
    failures = test_runner.run_tests(test_labels)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/test/runner.py", line 210, in run_tests
    old_config = self.setup_databases()
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/test/runner.py", line 166, in setup_databases
    **kwargs
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/test/runner.py", line 370, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/db/backends/base/creation.py", line 368, in create_test_db
    test_flush=True,
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/core/management/__init__.py", line 120, in call_command
    return command.execute(*args, **defaults)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 179, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 309, in sync_apps
    editor.create_model(model)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 282, in create_model
    self.execute(sql, params or None)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 107, in execute
    cursor.execute(sql, params)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
    raise value.with_traceback(tb)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/MySQLdb/cursors.py", line 226, in execute
    self.errorhandler(self, exc, value)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/MySQLdb/cursors.py", line 217, in execute
    res = self._query(query)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/MySQLdb/cursors.py", line 378, in _query
    rowcount = self._do_query(q)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/MySQLdb/cursors.py", line 341, in _do_query
    db.query(q)
  File "/home/xxx/virtualenv/myproject/lib/python3.4/site-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'None, None) NOT NULL)' at line 1")

【问题讨论】:

    标签: python mysql sql django testing


    【解决方案1】:

    导致错误是因为测试模型中的 Decimal Field 缺少两个必要的参数(max_digitsdecimal_places)。在使用 SQLite 后端时,这一点被忽略了。

    仍然不知道如何检查为测试模型自动创建的 SQL,因此任何有关此问题的提示将不胜感激。

    【讨论】:

    • 这有帮助。有时自定义或第 3 方字段不会显示正在发生的简单错误,例如在字符上忘记 max_length
    猜你喜欢
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 2020-03-18
    • 2017-04-21
    相关资源
    最近更新 更多