I'm not entirely sure whether this is an issue with taggit or Django itself, but since I can only reproduce it with taggit I'm reporting it here. Feel free to close as a Django issue if you think this isn't anything to do with taggit.
When using a through model with taggit and Django 1.7.2, migrations always fail with an OperationalError. This only occurs (as far as I know) with the sqlite backend, PostgreSQL, at least, is fine.
To reproduce
Create a simple project with an app with the following models.py
from django.db import models
from taggit.managers import TaggableManager
from taggit.models import TaggedItemBase
class BugTag(TaggedItemBase):
content_object = models.ForeignKey('Bug')
note = models.CharField(max_length=50)
class Bug(models.Model):
name = models.CharField(max_length=100)
tags = TaggableManager(through=BugTag)
Make your initial migrations with manage.py makemigrations <appname>
then run with manage.py migrate
.
The migrate
command will fail with an exception like:
Operations to perform:
Apply all migrations: sessions, admin, auth, contenttypes, taggit, bug
Running migrations:
Applying bug.0001_initial...Traceback (most recent call last):
File "./manage.py", line 7, in <module>
execute_from_command_line(sys.argv)
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 161, in handle
executor.migrate(targets, plan, fake=options.get("fake", False))
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 68, in migrate
self.apply_migration(migration, fake=fake)
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 102, in apply_migration
migration.apply(project_state, schema_editor)
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/db/migrations/migration.py", line 108, in apply
operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 37, in database_forwards
field,
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 177, in add_field
self._remake_table(model, create_fields=[field])
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 145, in _remake_table
self.quote_name(model._meta.db_table),
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/db/backends/schema.py", line 103, in execute
cursor.execute(sql, params)
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/home/username/.virtualenvs/sqlitebug/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 485, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: table bug_bug__new has no column named tags
The SQL causing the issue is:
SELECT name FROM sqlite_master
WHERE type in ('table', 'view') AND NOT name='sqlite_sequence'
ORDER BY name
BEGIN
CREATE TABLE "bug_bug" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(100) NOT NULL)
CREATE TABLE "bug_bugtag" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "note" varchar(50) NOT NULL, "content_object_id" integer NOT NULL REFERENCES "bug_bug" ("id"), "tag_id" integer NOT NULL REFERENCES "taggit_tag" ("id"))
CREATE TABLE "bug_bug__new" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(100) NOT NULL)
INSERT INTO "bug_bug__new" ("tags", "id", "name") SELECT NULL, "id", "name" FROM "bug_bug"
Django 1.7.1 doesn't exhibit the issue, and it's present in both the develop branch of taggit and the latest, 0.12.2, release.