简体   繁体   English

尝试在Django 1.9中迁移 - 奇怪的SQL错误“django.db.utils.OperationalError:near”)“:语法错误”

[英]Trying to migrate in Django 1.9 — strange SQL error “django.db.utils.OperationalError: near ”)“: syntax error”

I don't have a clue what's causing this error. 我不知道是什么导致了这个错误。 It appears to be a bug that there isn't a fix for. 它似乎是一个没有修复的错误。 Could anyone tell give me a hint as to how I might get around this? 任何人都可以告诉我如何解决这个问题吗? It's frustrating me to no end. 令我感到沮丧的是,我永无止境。 Thanks. 谢谢。

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

Here's the contents of 0006_auto_20160621_1640.py 这是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',
        ),
    ]

Here's the SQL produced from running 'python manage.py sqlmigrate optilab 0006' 这是运行'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;

This appears to be the line that's causing the errror: 这似乎是引起错误的行:

 INSERT INTO "optilab_lasersubstrate" () SELECT  FROM "optilab_lasersubstrate__old";

You are usually expected to have a list of columns in those parenthesis. 通常需要在这些括号中包含列的列表。 Eg INSERT INTO "optilab_lasersubstrate" (col1,col2,etc) however the migration has produced a blank set! 例如INSERT INTO "optilab_lasersubstrate" (col1,col2,etc)但是迁移产生了一个空白集! Similarly the SELECT FROM portion should read as SELECT col1,col2 FROM . 类似地, SELECT FROM部分应该读作SELECT col1,col2 FROM By some strange set of events you appear to have managed to create a table with no columns!! 通过一些奇怪的事件,你似乎设法创建一个没有列的表!

I see from your migration file that you are anyway dropping this table. 我从你的迁移文件中看到你无论如何都放弃了这个表。 So there isn't any reason to struggle with the RemoveField portion. 所以没有任何理由与RemoveField部分斗争。 It's code associated with the RemoveField that's causing the error. 它是与导致错误的RemoveField相关联的代码。 Change your migration as follows: 按如下方式更改迁移:

class Migration(migrations.Migration):

    dependencies = [
        ('optilab', '0005_test'),
    ]

    operations = [
        migrations.DeleteModel(
            name='LaserSubstrate',
        ),
        migrations.DeleteModel(
            name='WaveguideSubstrate',
        ),
    ]

Edit base.py in the lines that breaks and update it to: 在打破的行中编辑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)

暂无
暂无

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

相关问题 django.db.utils.OperationalError:接近񐁂򐁇N:语法错误 - django.db.utils.OperationalError: near 񐁂򐁇N: syntax error django.db.utils.OperationalError:“ N”附近:语法错误 - django.db.utils.OperationalError: near “񐁂򐁇N”: syntax error makemigrations/migrate 错误 django.db.utils.OperationalError:没有这样的表 - makemigrations/migrate error django.db.utils.OperationalError: no such table 如何修复“ django.db.utils.OperationalError:靠近“无”:语法错误” db.sqlite3? - How to fix 'django.db.utils.OperationalError: near “None”: syntax error' db.sqlite3? 使用 PostgreSQL 9.5 在 Django 1.9 中执行“python manage.py migrate”时出现错误“django.db.utils.OperationalError” - Getting error 'django.db.utils.OperationalError' when doing 'python manage.py migrate' in Django 1.9 with PostgreSQL 9.5 迁移错误(django.db.utils.OperationalError) - Migration error(django.db.utils.OperationalError) Makemigrations 错误:django.db.utils.OperationalError:没有这样的表 - Makemigrations error : django.db.utils.OperationalError: no such table 试图在 ubuntu 上为 django 设置 mysql 但出现错误 'django.db.utils.OperationalError: (1046, 'No database selected') - trying to setup mysql on ubuntu for django but getting error 'django.db.utils.OperationalError: (1046, 'No database selected')' 更改为PostgreSQL会引发django.db.utils.OperationalError:没有此类表错误 - Change to PostgreSQL raise django.db.utils.OperationalError: no such table Error 什么是 django.db.utils.OperationalError:(2000,'未知的 MySQL 错误') - What is django.db.utils.OperationalError: (2000, 'Unknown MySQL error')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM