Commits

shaib committed 21bf672

Add datetime default workaround for sqlserver and test for default datetime
instead of faulty test in test_sql_defaults

Comments (0)

Files changed (2)

south/db/sql_server/pyodbc.py

         else:
             #TODO: Anybody else needs special translations?
             return str(value) 
+    def _default_value_workaround(self, value):
+        if isinstance(value, (date,time,datetime)):
+            return value.isoformat()
+        else:
+            return super(DatabaseOperations, self)._default_value_workaround(value)
         
     def _quote_string(self, s):
         return "'" + s.replace("'","''") + "'"

south/tests/db.py

             ('textcol', models.TextField(blank=True)),
         ])
 
+    def test_datetime_default(self):
+        """
+        Test that defaults are created correctly for datetime columns
+        """    
+        from datetime import datetime
+
+        end_of_world = datetime(2012, 12, 21, 0, 0, 1)
+  
+        db.create_table("test_datetime_def", [
+            ('col0', models.IntegerField(null=True)),
+            ('col1', models.DateTimeField(default=end_of_world)),
+            ('col2', models.DateTimeField(null=True)),
+        ])
+        db.alter_column("test_datetime_def", "col2", models.DateTimeField(default=end_of_world))        
+        db.add_column("test_datetime_def", "col3", models.DateTimeField(default=end_of_world))
+        db.execute("insert into test_datetime_def (col0) values (null)")
+        ends = db.execute("select col1,col2,col3 from test_datetime_def")[0]
+        self.failUnlessEqual(len(ends), 3)
+        for e in ends:
+            self.failUnlessEqual(e, end_of_world)
+        
     def test_add_unique_fk(self):
         """
         Test adding a ForeignKey with unique=True or a OneToOneField
 
         false_value = db.has_booleans and 'False' or '0' 
         defaults = (
-            (models.DateTimeField(default=datetime(2012, 12, 21, 0, 0, 1)), 'DEFAULT \'2012-12-21 00:00:01'),
+            #(models.DateTimeField(default=datetime(2012, 12, 21, 0, 0, 1)), 'DEFAULT \'2012-12-21 00:00:01'), # replaced by test_datetime_default
             (models.CharField(default='sukasuka'), 'DEFAULT \'sukasuka'),
             (models.BooleanField(default=False), 'DEFAULT %s' % false_value),
             (models.IntegerField(default=42), 'DEFAULT 42'),