繁体   English   中英

如何查看在Django中为测试模型创建的SQL?

[英]How to view the SQL created for test models in Django?

在当前的Django项目中,每次运行测试时,我都会收到django.db.utils.ProgrammingError ,但前提是使用MySQL后端( 对于Python 3使用mysqlclient ); 如果使用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应用程序(带有两个后端)时,项目(网站和管理员)仍然可以正常运行。 该错误仅在运行测试时发生。

我在核心程序包中有一个models.py ,它定义了一些其他应用程序使用的抽象模型和mixin。 该软件包中还有一个test.py ,其中定义了一些模型,这些模型使用了这些抽象类和mixins,因此我可以对其进行测试。 我想错误在那里……某处; 但我无法从异常中弄清楚。

由于“真实”模型(不是抽象模型)仅存在于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")

该错误已造成,因为一个十进制字段在测试模型缺少两个参数,可在必要时(max_digitsdecimal_places)。 使用SQLite后端时,这一点被忽视。

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

暂无
暂无

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

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