Commits

Carl Meyer  committed 684edc7

set default value correctly for added columns in SQLite; fixes #447

  • Participants
  • Parent commits 898fefd

Comments (0)

Files changed (2)

File south/db/sqlite3.py

         "Used to copy data into a new table"
         # Make a list of all the fields to select
         cursor = self._get_connection().cursor()
-        q_fields = [column_info[0] for column_info in self._get_connection().introspection.get_table_description(cursor, dst)]
-        new_to_old = {}
-        for old, new in field_renames.items():
-            new_to_old[new] = old
-        q_fields_new = []
-        for field in q_fields:
-            if field in new_to_old:
-                # Make sure renames are done correctly
-                q_fields_new.append("%s AS %s" % (self.quote_name(new_to_old[field]), self.quote_name(field)))
+        src_fields = [column_info[0] for column_info in self._get_connection().introspection.get_table_description(cursor, src)]
+        dst_fields = [column_info[0] for column_info in self._get_connection().introspection.get_table_description(cursor, dst)]
+        src_fields_new = []
+        dst_fields_new = []
+        for field in src_fields:
+            if field in field_renames:
+                dst_fields_new.append(self.quote_name(field_renames[field]))
+            elif field in dst_fields:
+                dst_fields_new.append(self.quote_name(field))
             else:
-                q_fields_new.append(self.quote_name(field))
-        q_fields = q_fields_new
+                continue
+            src_fields_new.append(self.quote_name(field))
         # Copy over the data
-        self.execute("INSERT INTO %s SELECT %s FROM %s;" % (
+        self.execute("INSERT INTO %s (%s) SELECT %s FROM %s;" % (
             self.quote_name(dst),
-            ', '.join(q_fields),
+            ', '.join(dst_fields_new),
+            ', '.join(src_fields_new),
             self.quote_name(src),
         ))
     

File south/tests/db.py

         db.add_column("test_addc", "add1", models.IntegerField(default=3), keep_default=False)
         # Add a FK with keep_default=False (#69)
         User = db.mock_model(model_name='User', db_table='auth_user', db_tablespace='', pk_field_name='id', pk_field_type=models.AutoField, pk_field_args=[], pk_field_kwargs={})
+        # insert some data so we can test the default value of the added fkey
+        db.execute("INSERT INTO test_addc (eggs, add1) VALUES (1, 2)")
         db.add_column("test_addc", "user", models.ForeignKey(User, null=True), keep_default=False)
         # try selecting from the user_id column to make sure it was actually created
-        db.execute("SELECT user_id FROM test_addc")
+        val = db.execute("SELECT user_id FROM test_addc")[0][0]
+        self.assertEquals(val, None)
         db.delete_column("test_addc", "add1")
         db.delete_table("test_addc")