Opened 20 months ago

Closed 20 months ago

Last modified 20 months ago

#22359 closed Bug (fixed)

Changing m2m field to blank=True makes failed migration on sqlite

Reported by: melinath Owned by: nobody
Component: Migrations Version: 1.7-beta-1
Severity: Release blocker Keywords:
Cc: melinath, adrien, loic@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


If I change a M2M field by adding blank=True, the migration that django creates fails on SQLite. Specifically, it tries to re-create the M2M table (even though it already exists.)

I find this especially strange since adding blank=True to an M2M field actually doesn't change the database structure at all.

Migration file:

# encoding: utf8
from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('brambling', '0021_auto_20140330_1917'),

    operations = [
            field=models.ManyToManyField(to='brambling.EventType', blank=True),
            field=models.ManyToManyField(to='brambling.DanceStyle', blank=True),


  File "./", line 10, in <module>
  File ".../django/django/core/management/", line 427, in execute_from_command_line
  File ".../django/django/core/management/", line 419, in execute
  File ".../django/django/core/management/", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ".../django/django/core/management/", line 337, in execute
    output = self.handle(*args, **options)
  File ".../django/django/core/management/commands/", line 145, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File ".../django/django/db/migrations/", line 60, in migrate
    self.apply_migration(migration, fake=fake)
  File ".../django/django/db/migrations/", line 94, in apply_migration
    migration.apply(project_state, schema_editor)
  File ".../django/django/db/migrations/", line 97, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File ".../django/django/db/migrations/operations/", line 130, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File ".../django/django/db/backends/sqlite3/", line 143, in alter_field
    return self._alter_many_to_many(model, old_field, new_field, strict)
  File ".../django/django/db/backends/sqlite3/", line 168, in _alter_many_to_many
  File ".../django/django/db/backends/", line 253, in create_model
    self.execute(sql, params)
  File ".../django/django/db/backends/", line 95, in execute
    cursor.execute(sql, params)
  File ".../django/django/db/backends/", line 77, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File ".../django/django/db/backends/", line 61, in execute
    return self.cursor.execute(sql, params)
  File ".../django/django/db/", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File ".../django/django/db/backends/", line 61, in execute
    return self.cursor.execute(sql, params)
  File ".../django/django/db/backends/sqlite3/", line 479, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: table "brambling_person_event_types" already exists

Change History (5)

comment:1 Changed 20 months ago by adrien

  • Cc adrien added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

I'm able to reproduce.

Issue is with the through table. Assuming it needs to be recreated under a different name?

Last edited 20 months ago by adrien (previous) (diff)

comment:2 Changed 20 months ago by loic84

  • Cc loic@… added
  • Severity changed from Normal to Release blocker

comment:3 Changed 20 months ago by loic84

  • Has patch set

@melinath, @adrien, could you please check if solves your problem?

comment:4 Changed 20 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In a449e7feec73c7a14a972721942ce0f649c9d875:

Fixed #22359 -- Changing M2M field to blank=True failed on sqlite.

comment:5 Changed 20 months ago by Tim Graham <timograham@…>

In bdec84806365209bf800e9213a1cbb9b5dc2aa29:

[1.7.x] Fixed #22359 -- Changing M2M field to blank=True failed on sqlite.

Backport of a449e7feec from master

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