mirror of
				https://github.com/django/django.git
				synced 2025-10-31 09:41:08 +00:00 
			
		
		
		
	Fixed #21783: (again) Found second source of bug, also squashed it.
This commit is contained in:
		@@ -27,7 +27,15 @@ class AddField(Operation):
 | 
				
			|||||||
        from_model = from_state.render().get_model(app_label, self.model_name)
 | 
					        from_model = from_state.render().get_model(app_label, self.model_name)
 | 
				
			||||||
        to_model = to_state.render().get_model(app_label, self.model_name)
 | 
					        to_model = to_state.render().get_model(app_label, self.model_name)
 | 
				
			||||||
        if router.allow_migrate(schema_editor.connection.alias, to_model):
 | 
					        if router.allow_migrate(schema_editor.connection.alias, to_model):
 | 
				
			||||||
            schema_editor.add_field(from_model, to_model._meta.get_field_by_name(self.name)[0])
 | 
					            field = to_model._meta.get_field_by_name(self.name)[0]
 | 
				
			||||||
 | 
					            if not self.preserve_default:
 | 
				
			||||||
 | 
					                field.default = self.field.default
 | 
				
			||||||
 | 
					            schema_editor.add_field(
 | 
				
			||||||
 | 
					                from_model,
 | 
				
			||||||
 | 
					                field,
 | 
				
			||||||
 | 
					            )
 | 
				
			||||||
 | 
					            if not self.preserve_default:
 | 
				
			||||||
 | 
					                field.default = NOT_PROVIDED
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def database_backwards(self, app_label, schema_editor, from_state, to_state):
 | 
					    def database_backwards(self, app_label, schema_editor, from_state, to_state):
 | 
				
			||||||
        from_model = from_state.render().get_model(app_label, self.model_name)
 | 
					        from_model = from_state.render().get_model(app_label, self.model_name)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -184,6 +184,14 @@ class OperationTests(MigrationTestBase):
 | 
				
			|||||||
            if n == "height"
 | 
					            if n == "height"
 | 
				
			||||||
        ][0]
 | 
					        ][0]
 | 
				
			||||||
        self.assertEqual(field.default, NOT_PROVIDED)
 | 
					        self.assertEqual(field.default, NOT_PROVIDED)
 | 
				
			||||||
 | 
					        # Test the database alteration
 | 
				
			||||||
 | 
					        project_state.render().get_model("test_adflpd", "pony").objects.create(
 | 
				
			||||||
 | 
					            weight = 4,
 | 
				
			||||||
 | 
					        )
 | 
				
			||||||
 | 
					        self.assertColumnNotExists("test_adflpd_pony", "height")
 | 
				
			||||||
 | 
					        with connection.schema_editor() as editor:
 | 
				
			||||||
 | 
					            operation.database_forwards("test_adflpd", editor, project_state, new_state)
 | 
				
			||||||
 | 
					        self.assertColumnExists("test_adflpd_pony", "height")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_add_field_m2m(self):
 | 
					    def test_add_field_m2m(self):
 | 
				
			||||||
        """
 | 
					        """
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user