Skip to content

Commit

Permalink
Fixed #22359 -- Changing M2M field to blank=True failed on sqlite.
Browse files Browse the repository at this point in the history
  • Loading branch information
loic authored and timgraham committed Mar 31, 2014
1 parent cb5dd99 commit a449e7f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion django/db/backends/sqlite3/schema.py
Expand Up @@ -161,7 +161,7 @@ def _alter_many_to_many(self, model, old_field, new_field, strict):
""" """
Alters M2Ms to repoint their to= endpoints. Alters M2Ms to repoint their to= endpoints.
""" """
if old_field.rel.through == new_field.rel.through: if old_field.rel.through._meta.db_table == new_field.rel.through._meta.db_table:
return return


# Make a new through table # Make a new through table
Expand Down
38 changes: 30 additions & 8 deletions tests/migrations/test_operations.py
Expand Up @@ -14,6 +14,17 @@ class OperationTests(MigrationTestBase):
both forwards and backwards. both forwards and backwards.
""" """


def apply_operations(self, app_label, project_state, operations):
new_state = project_state.clone()
for operation in operations:
operation.state_forwards(app_label, new_state)

# Set up the database
with connection.schema_editor() as editor:
for operation in operations:
operation.database_forwards(app_label, editor, project_state, new_state)
return new_state

def set_up_test_model(self, app_label, second_model=False, related_model=False, mti_model=False): def set_up_test_model(self, app_label, second_model=False, related_model=False, mti_model=False):
""" """
Creates a test model state and database table. Creates a test model state and database table.
Expand Down Expand Up @@ -67,14 +78,8 @@ def set_up_test_model(self, app_label, second_model=False, related_model=False,
], ],
bases=['%s.Pony' % app_label], bases=['%s.Pony' % app_label],
)) ))
project_state = ProjectState()
for operation in operations: return self.apply_operations(app_label, ProjectState(), operations)
operation.state_forwards(app_label, project_state)
# Set up the database
with connection.schema_editor() as editor:
for operation in operations:
operation.database_forwards(app_label, editor, ProjectState(), project_state)
return project_state


def test_create_model(self): def test_create_model(self):
""" """
Expand Down Expand Up @@ -374,6 +379,23 @@ def test_add_field_m2m(self):
operation.database_backwards("test_adflmm", editor, new_state, project_state) operation.database_backwards("test_adflmm", editor, new_state, project_state)
self.assertTableNotExists("test_adflmm_pony_stables") self.assertTableNotExists("test_adflmm_pony_stables")


def test_alter_field_m2m(self):
project_state = self.set_up_test_model("test_alflmm", second_model=True)

project_state = self.apply_operations("test_alflmm", project_state, operations=[
migrations.AddField("Pony", "stables", models.ManyToManyField("Stable", related_name="ponies"))
])
new_apps = project_state.render()
Pony = new_apps.get_model("test_alflmm", "Pony")
self.assertFalse(Pony._meta.get_field('stables').blank)

project_state = self.apply_operations("test_alflmm", project_state, operations=[
migrations.AlterField("Pony", "stables", models.ManyToManyField(to="Stable", related_name="ponies", blank=True))
])
new_apps = project_state.render()
Pony = new_apps.get_model("test_alflmm", "Pony")
self.assertTrue(Pony._meta.get_field('stables').blank)

def test_remove_field(self): def test_remove_field(self):
""" """
Tests the RemoveField operation. Tests the RemoveField operation.
Expand Down

0 comments on commit a449e7f

Please sign in to comment.