コード例 #1
0
ファイル: tests.py プロジェクト: yellowcap/django
 def test_alter_null_to_not_null(self):
     """
     #23609 - Tests handling of default values when altering from NULL to NOT NULL.
     """
     # Create the table
     with connection.schema_editor() as editor:
         editor.create_model(Author)
     # Ensure the field is right to begin with
     columns = self.column_classes(Author)
     self.assertTrue(columns['height'][1][6])
     # Create some test data
     Author.objects.create(name='Not null author', height=12)
     Author.objects.create(name='Null author')
     # Verify null value
     self.assertEqual(Author.objects.get(name='Not null author').height, 12)
     self.assertIsNone(Author.objects.get(name='Null author').height)
     # Alter the height field to NOT NULL with default
     new_field = PositiveIntegerField(default=42)
     new_field.set_attributes_from_name("height")
     with connection.schema_editor() as editor:
         editor.alter_field(Author,
                            Author._meta.get_field_by_name("height")[0],
                            new_field)
     # Ensure the field is right afterwards
     columns = self.column_classes(Author)
     self.assertFalse(columns['height'][1][6])
     # Verify default value
     self.assertEqual(Author.objects.get(name='Not null author').height, 12)
     self.assertEqual(Author.objects.get(name='Null author').height, 42)
コード例 #2
0
ファイル: tests.py プロジェクト: DasAllFolks/django
 def test_alter_null_to_not_null(self):
     """
     #23609 - Tests handling of default values when altering from NULL to NOT NULL.
     """
     # Create the table
     with connection.schema_editor() as editor:
         editor.create_model(Author)
     # Ensure the field is right to begin with
     columns = self.column_classes(Author)
     self.assertTrue(columns['height'][1][6])
     # Create some test data
     Author.objects.create(name='Not null author', height=12)
     Author.objects.create(name='Null author')
     # Verify null value
     self.assertEqual(Author.objects.get(name='Not null author').height, 12)
     self.assertIsNone(Author.objects.get(name='Null author').height)
     # Alter the height field to NOT NULL with default
     new_field = PositiveIntegerField(default=42)
     new_field.set_attributes_from_name("height")
     with connection.schema_editor() as editor:
         editor.alter_field(
             Author,
             Author._meta.get_field_by_name("height")[0],
             new_field
         )
     # Ensure the field is right afterwards
     columns = self.column_classes(Author)
     self.assertFalse(columns['height'][1][6])
     # Verify default value
     self.assertEqual(Author.objects.get(name='Not null author').height, 12)
     self.assertEqual(Author.objects.get(name='Null author').height, 42)
コード例 #3
0
ファイル: tests.py プロジェクト: yellowcap/django
 def test_alter_null_to_not_null_keeping_default(self):
     """
     #23738 - Can change a nullable field with default to non-nullable
     with the same default.
     """
     # Create the table
     with connection.schema_editor() as editor:
         editor.create_model(AuthorWithDefaultHeight)
     # Ensure the field is right to begin with
     columns = self.column_classes(AuthorWithDefaultHeight)
     self.assertTrue(columns['height'][1][6])
     # Alter the height field to NOT NULL keeping the previous default
     new_field = PositiveIntegerField(default=42)
     new_field.set_attributes_from_name("height")
     with connection.schema_editor() as editor:
         editor.alter_field(
             AuthorWithDefaultHeight,
             AuthorWithDefaultHeight._meta.get_field_by_name("height")[0],
             new_field,
         )
     # Ensure the field is right afterwards
     columns = self.column_classes(AuthorWithDefaultHeight)
     self.assertFalse(columns['height'][1][6])
コード例 #4
0
ファイル: tests.py プロジェクト: DasAllFolks/django
 def test_alter_null_to_not_null_keeping_default(self):
     """
     #23738 - Can change a nullable field with default to non-nullable
     with the same default.
     """
     # Create the table
     with connection.schema_editor() as editor:
         editor.create_model(AuthorWithDefaultHeight)
     # Ensure the field is right to begin with
     columns = self.column_classes(AuthorWithDefaultHeight)
     self.assertTrue(columns['height'][1][6])
     # Alter the height field to NOT NULL keeping the previous default
     new_field = PositiveIntegerField(default=42)
     new_field.set_attributes_from_name("height")
     with connection.schema_editor() as editor:
         editor.alter_field(
             AuthorWithDefaultHeight,
             AuthorWithDefaultHeight._meta.get_field_by_name("height")[0],
             new_field,
         )
     # Ensure the field is right afterwards
     columns = self.column_classes(AuthorWithDefaultHeight)
     self.assertFalse(columns['height'][1][6])