Opened 15 months ago

Closed 15 months ago

Last modified 15 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

Description

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 = [
        migrations.AlterField(
            model_name='person',
            name='event_types',
            field=models.ManyToManyField(to='brambling.EventType', blank=True),
        ),
        migrations.AlterField(
            model_name='person',
            name='dance_styles',
            field=models.ManyToManyField(to='brambling.DanceStyle', blank=True),
        ),
    ]

Traceback:

  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File ".../django/django/core/management/__init__.py", line 427, in execute_from_command_line
    utility.execute()
  File ".../django/django/core/management/__init__.py", line 419, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File ".../django/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File ".../django/django/core/management/base.py", line 337, in execute
    output = self.handle(*args, **options)
  File ".../django/django/core/management/commands/migrate.py", line 145, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File ".../django/django/db/migrations/executor.py", line 60, in migrate
    self.apply_migration(migration, fake=fake)
  File ".../django/django/db/migrations/executor.py", line 94, in apply_migration
    migration.apply(project_state, schema_editor)
  File ".../django/django/db/migrations/migration.py", line 97, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File ".../django/django/db/migrations/operations/fields.py", line 130, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File ".../django/django/db/backends/sqlite3/schema.py", line 143, in alter_field
    return self._alter_many_to_many(model, old_field, new_field, strict)
  File ".../django/django/db/backends/sqlite3/schema.py", line 168, in _alter_many_to_many
    self.create_model(new_field.rel.through)
  File ".../django/django/db/backends/schema.py", line 253, in create_model
    self.execute(sql, params)
  File ".../django/django/db/backends/schema.py", line 95, in execute
    cursor.execute(sql, params)
  File ".../django/django/db/backends/utils.py", line 77, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File ".../django/django/db/backends/utils.py", line 61, in execute
    return self.cursor.execute(sql, params)
  File ".../django/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File ".../django/django/db/backends/utils.py", line 61, in execute
    return self.cursor.execute(sql, params)
  File ".../django/django/db/backends/sqlite3/base.py", 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 15 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 15 months ago by adrien (previous) (diff)

comment:2 Changed 15 months ago by loic84

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

comment:3 Changed 15 months ago by loic84

  • Has patch set

@melinath, @adrien, could you please check if https://github.com/django/django/pull/2499 solves your problem?

comment:4 Changed 15 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 15 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