尝试在 Django 1.9 中迁移——奇怪的 SQL 错误 "django.db.utils.OperationalError: near ")": syntax er
Posted
技术标签:
【中文标题】尝试在 Django 1.9 中迁移——奇怪的 SQL 错误 "django.db.utils.OperationalError: near ")": syntax error"【英文标题】:Trying to migrate in Django 1.9 -- strange SQL error "django.db.utils.OperationalError: near ")": syntax error" 【发布时间】:2016-10-23 16:25:26 【问题描述】:我不知道是什么导致了这个错误。这似乎是一个没有修复的错误。谁能告诉我如何解决这个问题?这让我很沮丧。谢谢。
Operations to perform:
Apply all migrations: admin, contenttypes, optilab, auth, sessions
Running migrations:
Rendering model states... DONE
Applying optilab.0006_auto_20160621_1640...Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
utility.execute()
File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 345, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Python27\lib\site-packages\django\core\management\base.py", line 399, in execute
output = self.handle(*args, **options)
File "C:\Python27\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 92, in migrate
self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 121, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:\Python27\lib\site-packages\django\db\migrations\executor.py", line 198, in apply_migration
state = migration.apply(state, schema_editor)
File "C:\Python27\lib\site-packages\django\db\migrations\migration.py", line 123, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:\Python27\lib\site-packages\django\db\migrations\operations\fields.py", line 121, in database_forwards
schema_editor.remove_field(from_model, from_model._meta.get_field(self.name))
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\schema.py", line 247, in remove_field
self._remake_table(model, delete_fields=[field])
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\schema.py", line 197, in _remake_table
self.quote_name(model._meta.db_table),
File "C:\Python27\lib\site-packages\django\db\backends\base\schema.py", line 110, in execute
cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\utils.py", line 95, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py", line 323, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: near ")": syntax error
这是 0006_auto_20160621_1640.py 的内容
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-06-21 22:40
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('optilab', '0005_test'),
]
operations = [
migrations.RemoveField(
model_name='lasersubstrate',
name='substrate_ptr',
),
migrations.RemoveField(
model_name='waveguidesubstrate',
name='substrate_ptr',
),
migrations.DeleteModel(
name='LaserSubstrate',
),
migrations.DeleteModel(
name='WaveguideSubstrate',
),
]
这是运行'python manage.py sqlmigrate optilab 0006'产生的SQL
BEGIN;
--
-- Remove field substrate_ptr from lasersubstrate
--
ALTER TABLE "optilab_lasersubstrate" RENAME TO "optilab_lasersubstrate__old";
CREATE TABLE "optilab_lasersubstrate" ("substrate_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "optilab_substrate" ("id"));
INSERT INTO "optilab_lasersubstrate" () SELECT FROM "optilab_lasersubstrate__old";
DROP TABLE "optilab_lasersubstrate__old";
--
-- Remove field substrate_ptr from waveguidesubstrate
--
ALTER TABLE "optilab_waveguidesubstrate" RENAME TO "optilab_waveguidesubstrate__old";
CREATE TABLE "optilab_waveguidesubstrate" ("substrate_ptr_id" integer NOT NULL PRIMARY KEY REFERENCES "optilab_substrate" ("id"));
INSERT INTO "optilab_waveguidesubstrate" () SELECT FROM "optilab_waveguidesubstrate__old";
DROP TABLE "optilab_waveguidesubstrate__old";
--
-- Delete model LaserSubstrate
--
DROP TABLE "optilab_lasersubstrate";
--
-- Delete model WaveguideSubstrate
--
DROP TABLE "optilab_waveguidesubstrate";
COMMIT;
【问题讨论】:
@e4c5 抱歉,不知道那是什么,也不知道如何找到它。 使用find
命令或查看IDE 中的树视图很简单。但是,需要的是 optilab 的迁移文件夹中以 0006 开头的文件
@e4c5 感谢您的耐心等待。我正在使用文件编辑帖子...
@e4c5 它没有产生错误。
@e4c5 我包含了 sqlmigrate 输出。
【参考方案1】:
这似乎是导致错误的行:
INSERT INTO "optilab_lasersubstrate" () SELECT FROM "optilab_lasersubstrate__old";
通常希望您在这些括号中有一个列列表。例如INSERT INTO "optilab_lasersubstrate" (col1,col2,etc)
但是迁移产生了一个空白集!同样,SELECT FROM
部分应该读作SELECT col1,col2 FROM
。通过一些奇怪的事件,您似乎设法创建了一个没有列的表!
我从您的迁移文件中看到,您无论如何都会删除此表。所以没有任何理由与RemoveField
部分抗争。与 RemoveField
相关的代码会导致错误。如下更改您的迁移:
class Migration(migrations.Migration):
dependencies = [
('optilab', '0005_test'),
]
operations = [
migrations.DeleteModel(
name='LaserSubstrate',
),
migrations.DeleteModel(
name='WaveguideSubstrate',
),
]
【讨论】:
很高兴能帮上忙 我也遇到了这个问题。这只是对原因的猜测 - 我创建并迁移了一个只是占位符class Mumble: pass
的应用程序。它有 no 字段。
嗯,这也发生在我身上,但我所做的只是试图删除很多我不再使用的模型(它们与 Django-CMS 插件有关)
顺便说一句,我的迁移有 DeleteModel
命令,但只是在一些(不必要的?)RemoveFields
这似乎是问题的原因之后。【参考方案2】:
在中断的行中编辑 base.py 并将其更新为:
def execute(self, query, params=None):
if params is None:
if '()' not in str(query):
return Database.Cursor.execute(self, query)
query = self.convert_query(query)
if '()' not in str(query):
return Database.Cursor.execute(self, query, params)
【讨论】:
以上是关于尝试在 Django 1.9 中迁移——奇怪的 SQL 错误 "django.db.utils.OperationalError: near ")": syntax er的主要内容,如果未能解决你的问题,请参考以下文章
Python Django 1.9 迁移错误“创建新内容类型时出错...”