web-dev-qa-db-ja.com

Django 1.9-奇妙なSQLエラー "Django.db.utils.OperationalError:near") ":構文エラー"で移行しようとしています

このエラーの原因がわかりません。修正されていないバグのようです。誰かが私がこれを回避する方法についてのヒントを教えてもらえますか?それは終わりのない私を苛立たせています。ありがとう。

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',
        ),
    ]

これが「pythonmanage.pysqlmigrateoptilab0006」の実行から生成された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;
14
David J.

これは、エラーを引き起こしている行のようです。

_ 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',
        ),
    ]
_
23
e4c5

中断する行の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)
0
Adel