Opened 10 years ago

Last modified 10 years ago

#24061 closed Cleanup/optimization

SQLite backend: Deleting last field from model: migration generates SQL with Syntax Error — at Initial Version

Reported by: Aur Saraf Owned by: nobody
Component: Migrations Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

$ pip freeze
Django==1.7.1
wsgiref==0.1.2

I add a model that inherits from another model, makemigrations, delete that model, makemigrations, and the SQLite backend generates this illegal SQL when trying to delete the last field "model_ptr" from the table:

INSERT INTO "bug_b__new" () SELECT  FROM "bug_b"

Easy repro:

$ mkvirtualenv bug
$ git clone git@github.com:SonOfLilit/django-sqlite-migrations-bug.git
$ cd django-sqlite-migrations-bug
$ python manage.py test

Here is what happens:

$ python -m pdb manage.py test
> d:\projects\galago\bug\sqlitebug\manage.py(2)<module>()
-> import os
(Pdb) c
Creating test database for alias 'default'...
Traceback (most recent call last):
  File "c:\Python27\Lib\pdb.py", line 1314, in main
    pdb._runscript(mainpyfile)
  File "c:\Python27\Lib\pdb.py", line 1233, in _runscript
    self.run(statement)
  File "c:\Python27\Lib\bdb.py", line 400, in run
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
  File "manage.py", line 2, in <module>
    import os
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\core\management\__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\core\management\commands\test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\core\management\commands\test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\core\management\base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\core\management\commands\test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\test\runner.py", line 147, in run_tests
    old_config = self.setup_databases()
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\test\runner.py", line 109, in setup_databases
    return setup_databases(self.verbosity, self.interactive, **kwargs)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\test\runner.py", line 299, in setup_databases
    serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\db\backends\creation.py", line 377, in create_test_db
    test_flush=True,
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\core\management\__init__.py", line 115, in call_command
    return klass.execute(*args, **defaults)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\core\management\base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\core\management\commands\migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\db\migrations\executor.py", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\db\migrations\executor.py", line 97, in apply_migration
    migration.apply(project_state, schema_editor)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\db\migrations\migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
    schema_editor.remove_field(from_model, from_model._meta.get_field_by_name(self.name)[0])
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\db\backends\sqlite3\schema.py", line 185, in remove_field
    self._remake_table(model, delete_fields=[field])
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\db\backends\sqlite3\schema.py", line 135, in _remake_table
    self.quote_name(model._meta.db_table),
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\db\backends\schema.py", line 99, in execute
    cursor.execute(sql, params)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "c:\Users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\db\backends\sqlite3\base.py", line 485, in execute
    return Database.Cursor.execute(self, query, params)
OperationalError: near ")": syntax error
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> c:\users\sonoflilit\.virtualenvs\galago\lib\site-packages\django\db\backends\sqlite3\base.py(485)execute()
-> return Database.Cursor.execute(self, query, params)
(Pdb) print query
INSERT INTO "bug_b__new" () SELECT  FROM "bug_b"

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top