Commits

shaib committed bc07bad

Fix #1262 -- allow empty strings as defaults in SQLite

Comments (0)

Files changed (2)

south/db/sqlite3.py

         field_default = None
         if not getattr(field, '_suppress_default', False):
             default = field.get_default()
-            if default is not None and default!='':
+            if default is not None:
                 field_default = "'%s'" % field.get_db_prep_save(default, connection=self._get_connection())
         field._suppress_default = True
         self._remake_table(table_name, added={
                 continue
             src_fields_new.append(self.quote_name(field))
         for field, (_,default) in added.items():
-            if default is not None and default!='':
+            if default is not None:
                 field = self.quote_name(field)
                 src_fields_new.append("%s as %s" % (default, field))
                 dst_fields_new.append(field)

south/tests/db.py

         db.execute("INSERT INTO test_altercd (eggs) values (12)")
         null = db.execute("SELECT spam FROM test_altercd")[0][0]
         self.assertFalse(null, "Default for char field was installed into database")
+
+    @skipIf('oracle' in db.backend_name, "Oracle does not differentiate empty trings from null")
+    def test_default_empty_string(self):
+        """
+        Test altering column defaults with char fields
+        """
+        db.create_table("test_cd_empty", [
+            ('spam', models.CharField(max_length=30, default='')),
+            ('eggs', models.CharField(max_length=30)),
+        ])
+        # Create a record
+        db.execute("INSERT INTO test_cd_empty (spam, eggs) values ('1','2')")
+        # Add a column
+        db.add_column("test_cd_empty", "ham", models.CharField(max_length=30, default=''))
+        
+        empty = db.execute("SELECT ham FROM test_cd_empty")[0][0]
+        self.assertEquals(empty, "", "Empty Default for char field isn't empty string")
         
     def test_mysql_defaults(self):
         """
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.