summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2014-10-23 12:24:34 -0400
committerTim Graham <timograham@gmail.com>2014-10-23 14:35:55 -0400
commit7750fc8fa80470e4c10b3753667e04f78ebe4781 (patch)
tree1927ad3814bcefbfaa06219c0ec7a43930ceb275
parentba6a599ce4e363cdce2d4cc439ce57436fb194fb (diff)
[1.7.x] Fixed #23702 -- Fixed adding an explicit id field on SQLite.
Thanks gavinwahl for the report. Backport of 92269b7b53 from master
-rw-r--r--django/db/backends/sqlite3/schema.py4
-rw-r--r--docs/releases/1.7.2.txt3
-rw-r--r--tests/schema/tests.py19
3 files changed, 24 insertions, 2 deletions
diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py
index 34e52f8417..7ed5449c8d 100644
--- a/django/db/backends/sqlite3/schema.py
+++ b/django/db/backends/sqlite3/schema.py
@@ -75,8 +75,8 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
)
# Add in any altered fields
for (old_field, new_field) in alter_fields:
- del body[old_field.name]
- del mapping[old_field.column]
+ body.pop(old_field.name, None)
+ mapping.pop(old_field.column, None)
body[new_field.name] = new_field
if old_field.null and not new_field.null:
case_sql = "coalesce(%(col)s, %(default)s)" % {
diff --git a/docs/releases/1.7.2.txt b/docs/releases/1.7.2.txt
index 47cc2d6bbd..32500045f0 100644
--- a/docs/releases/1.7.2.txt
+++ b/docs/releases/1.7.2.txt
@@ -11,3 +11,6 @@ Bugfixes
* Fixed migration's renaming of auto-created many-to-many tables when changing
:attr:`Meta.db_table <django.db.models.Options.db_table>` (:ticket:`23630`).
+
+* Fixed a migration crash when adding an explicit ``id`` field to a model on
+ SQLite (:ticket:`23702`).
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index 5b6a23bee0..3dc986e3a4 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -497,6 +497,25 @@ class SchemaTests(TransactionTestCase):
else:
self.fail("No FK constraint for author_id found")
+ def test_alter_implicit_id_to_explict(self):
+ """
+ Should be able to convert an implicit "id" field to an explicit "id"
+ primary key field.
+ """
+ with connection.schema_editor() as editor:
+ editor.create_model(Author)
+
+ new_field = IntegerField(primary_key=True)
+ new_field.set_attributes_from_name("id")
+ new_field.model = Author
+ with connection.schema_editor() as editor:
+ editor.alter_field(
+ Author,
+ Author._meta.get_field_by_name("id")[0],
+ new_field,
+ strict=True,
+ )
+
def test_rename(self):
"""
Tests simple altering of fields