| 498 | 498 | {self.option_name: self.option_value}, |
| 499 | 499 | ) |
| 500 | 500 | |
| 501 | def reduce(self, operation, app_label): | |
| 502 | """ | |
| 503 | Optimize together operations that remove and then add constraints. | |
| 504 | ||
| 505 | For example, optimize: | |
| 506 | [ | |
| 507 | AlterUniqueTogether(name='mymodel', unique_together=set()), | |
| 508 | AlterIndexTogether(name='mymodel', index_together=set()), | |
| 509 | AlterUniqueTogether(name='mymodel', unique_together={("col",)}), | |
| 510 | AlterIndexTogether(name='mymodel', index_together={("col",)}), | |
| 511 | ] | |
| 512 | to: | |
| 513 | [ | |
| 514 | AlterUniqueTogether(name='mymodel', unique_together={("col",)}), | |
| 515 | AlterIndexTogether(name='mymodel', index_together={("col",)}), | |
| 516 | ] | |
| 517 | """ | |
| 518 | # Handle the case where we have the same operation type | |
| 519 | if isinstance(operation, self.__class__) and self.name_lower == operation.name_lower: | |
| 520 | # If we're setting to empty and then to a value, just keep the value | |
| 521 | if self.option_value == set() and operation.option_value != set(): | |
| 522 | return [operation] | |
| 523 | # If we're setting to a value and then to empty, just keep the empty | |
| 524 | elif self.option_value != set() and operation.option_value == set(): | |
| 525 | return [operation] | |
| 526 | # Otherwise, keep the later operation (standard behavior) | |
| 527 | else: | |
| 528 | return [operation] | |
| 529 | ||
| 530 | # Handle cross-operation optimization between AlterUniqueTogether and AlterIndexTogether | |
| 531 | if (isinstance(operation, AlterTogetherOptionOperation) and | |
| 532 | self.name_lower == operation.name_lower and | |
| 533 | self.__class__ != operation.__class__): | |
| 534 | ||
| 535 | # If current operation sets to empty and the other sets to a value, | |
| 536 | # we can optimize by keeping only the value operation | |
| 537 | if self.option_value == set() and operation.option_value != set(): | |
| 538 | return [operation] | |
| 539 | ||
| 540 | # If current operation sets to a value and the other sets to empty, | |
| 541 | # we can optimize by keeping only the empty operation | |
| 542 | if self.option_value != set() and operation.option_value == set(): | |
| 543 | return [self] | |
| 544 | ||
| 545 | # If both operations are setting to empty, we can keep both (no optimization) | |
| 546 | if self.option_value == set() and operation.option_value == set(): | |
| 547 | return None | |
| 548 | ||
| 549 | # If both operations set to non-empty values, keep both | |
| 550 | if self.option_value != set() and operation.option_value != set(): | |
| 551 | return None | |
| 552 | ||
| 553 | return super().reduce(operation, app_label) | |
| 554 | ||
| 501 | 555 | def database_forwards(self, app_label, schema_editor, from_state, to_state): |
| 502 | 556 | new_model = to_state.apps.get_model(app_label, self.name) |
| 503 | 557 | if self.allow_migrate_model(schema_editor.connection.alias, new_model): |
| Test Name | Status |
|---|---|
index/unique_together also triggers on ordering changes. | Pass |
Removed fields will be removed after updating index/unique_together. | Pass |
Fields are renamed before updating index/unique_together. | Pass |
test_auto (migrations.test_autodetector.MigrationSuggestNameTests) | Pass |
test_many_operations_suffix (migrations.test_autodetector.MigrationSuggestNameTests) | Pass |
test_no_operations (migrations.test_autodetector.MigrationSuggestNameTests) | Pass |
test_no_operations_initial (migrations.test_autodetector.MigrationSuggestNameTests) | Pass |
test_none_name (migrations.test_autodetector.MigrationSuggestNameTests) | Pass |
test_none_name_with_initial_true (migrations.test_autodetector.MigrationSuggestNameTests) | Pass |
test_operation_with_no_suggested_name (migrations.test_autodetector.MigrationSuggestNameTests) | Pass |
test_single_operation (migrations.test_autodetector.MigrationSuggestNameTests) | Pass |
test_single_operation_long_name (migrations.test_autodetector.MigrationSuggestNameTests) | Pass |
test_two_create_models (migrations.test_autodetector.MigrationSuggestNameTests) | Pass |
test_two_create_models_with_initial_true (migrations.test_autodetector.MigrationSuggestNameTests) | Pass |
test_two_operations (migrations.test_autodetector.MigrationSuggestNameTests) | Pass |
Setting order_with_respect_to when adding the FK too does | Pass |
#23405 - Adding a NOT NULL and blank `CharField` or `TextField` | Pass |
Test change detection of new constraints. | Pass |
test_add_custom_fk_with_hardcoded_to (migrations.test_autodetector.AutodetectorTests) | Pass |
test_add_date_fields_with_auto_now_add_asking_for_default (migrations.test_autodetector.AutodetectorTests) | Pass |
test_add_date_fields_with_auto_now_add_not_asking_for_null_addition (migrations.test_autodetector.AutodetectorTests) | Pass |
test_add_date_fields_with_auto_now_not_asking_for_default (migrations.test_autodetector.AutodetectorTests) | Pass |
Tests autodetection of new fields. | Pass |
Added fields will be created before using them in index/unique_together. | Pass |
#22030 - Adding a field with a default should work. | Pass |
Tests index/unique_together detection. | Pass |
Test change detection of new indexes. | Pass |
#22435 - Adding a ManyToManyField should not prompt for a default. | Pass |
Setting order_with_respect_to when adding the whole model | Pass |
test_add_model_order_with_respect_to_index_constraint (migrations.test_autodetector.AutodetectorTests) | Pass |
test_add_model_order_with_respect_to_index_foo_together (migrations.test_autodetector.AutodetectorTests) | Pass |
Removing a base field takes place before adding a new inherited model | Pass |
#23405 - Adding a NOT NULL and non-blank `CharField` or `TextField` | Pass |
Tests detection for adding db_table in model's options. | Pass |
Tests detection for changing db_table in model's options'. | Pass |
Alter_db_table doesn't generate a migration if no changes have been made. | Pass |
Tests detection for removing db_table in model's options. | Pass |
Tests when model and db_table changes, autodetector must create two | Pass |
Fields are altered after deleting some index/unique_together. | Pass |
test_alter_field_to_fk_dependency_other_app (migrations.test_autodetector.AutodetectorTests) | Pass |
#23609 - Tests autodetection of nullable to non-nullable alterations. | Pass |
ForeignKeys are altered _before_ the model they used to | Pass |
test_alter_many_to_many (migrations.test_autodetector.AutodetectorTests) | Pass |
Changing the model managers adds a new operation. | Pass |
Changing a model's options should make a change. | Pass |
Changing a proxy model's options should also make a change. | Pass |
Tests auto-naming of migrations for graph matching. | Pass |
test_arrange_for_graph_with_multiple_initial (migrations.test_autodetector.AutodetectorTests) | Pass |
Bases of other models come first. | Pass |
test_bases_first_mixed_case_app_label (migrations.test_autodetector.AutodetectorTests) | Pass |
#23315 - The dependency resolver knows to put all CreateModel | Pass |
#23322 - The dependency resolver knows to explicitly resolve | Pass |
Having a circular ForeignKey dependency automatically | Pass |
#23938 - Changing a concrete field into a ManyToManyField | Pass |
test_create_model_and_unique_together (migrations.test_autodetector.AutodetectorTests) | Pass |
Test creation of new model with constraints already defined. | Pass |
Test creation of new model with indexes already defined. | Pass |
Adding a m2m with a through model and the models that use it should be | Pass |
Two instances which deconstruct to the same value aren't considered a | Pass |
Tests custom naming of migrations for graph matching. | Pass |
Field instances are handled correctly by nested deconstruction. | Pass |
#22951 -- Uninstantiated classes with deconstruct are correctly returned | Pass |
Nested deconstruction descends into dict values. | Pass |
Nested deconstruction descends into lists. | Pass |
Nested deconstruction descends into tuples. | Pass |
test_default_related_name_option (migrations.test_autodetector.AutodetectorTests) | Pass |
test_different_regex_does_alter (migrations.test_autodetector.AutodetectorTests) | Pass |
#23452 - Empty unique/index_together shouldn't generate a migration. | Pass |
A dependency to an app with no migrations uses __first__. | Pass |
Having a ForeignKey automatically adds a dependency. | Pass |
#23100 - ForeignKeys correctly depend on other apps' models. | Pass |
index/unique_together doesn't generate a migration if no | Pass |
Tests unique_together and field removal detection & ordering | Pass |
Removing an FK and the model it targets in the same change must remove | Pass |
test_identical_regex_doesnt_alter (migrations.test_autodetector.AutodetectorTests) | Pass |
Tests when model changes but db_table stays as-is, autodetector must not | Pass |
A dependency to an app with existing migrations uses the | Pass |
A model with a m2m field that specifies a "through" model cannot be | Pass |
test_managed_to_unmanaged (migrations.test_autodetector.AutodetectorTests) | Pass |
#23938 - Changing a ManyToManyField into a concrete field | Pass |
Removing a ManyToManyField and the "through" model in the same change | Pass |
Removing a model that contains a ManyToManyField and the "through" model | Pass |
test_mti_inheritance_model_removal (migrations.test_autodetector.AutodetectorTests) | Pass |
#23956 - Inheriting models doesn't move *_ptr fields into AddField operations. | Pass |
Nested deconstruction is applied recursively to the args/kwargs of | Pass |
Tests autodetection of new models. | Pass |
If two models with a ForeignKey from one to the other are removed at the | Pass |
Tests deletion of old models. | Pass |
Test change detection of reordering of fields in indexes. | Pass |
test_parse_number (migrations.test_autodetector.AutodetectorTests) | Pass |
test_partly_alter_foo_together (migrations.test_autodetector.AutodetectorTests) | Pass |
A relation used as the primary key is kept as part of CreateModel. | Pass |
The autodetector correctly deals with proxy models. | Pass |
Bases of proxies come first. | Pass |
#23415 - The autodetector must correctly deal with custom FK on proxy | Pass |
FK dependencies still work on proxy models. | Pass |
test_proxy_non_model_parent (migrations.test_autodetector.AutodetectorTests) | Pass |
test_proxy_to_mti_with_fk_to_proxy (migrations.test_autodetector.AutodetectorTests) | Pass |
test_proxy_to_mti_with_fk_to_proxy_proxy (migrations.test_autodetector.AutodetectorTests) | Pass |
Removing order_with_respect_to when removing the FK too does | Pass |
Test change detection of removed constraints. | Pass |
Tests autodetection of removed fields. | Pass |
Test change detection of removed indexes. | Pass |
Tests autodetection of renamed fields. | Pass |
test_rename_field_foreign_key_to_field (migrations.test_autodetector.AutodetectorTests) | Pass |
RenameField is used if a field is renamed and db_column equal to the | Pass |
test_rename_foreign_object_fields (migrations.test_autodetector.AutodetectorTests) | Pass |
Tests autodetection of renamed models that are used in M2M relations as | Pass |
Tests autodetection of renamed models. | Pass |
Model name is case-insensitive. Changing case doesn't lead to any | Pass |
The migration to rename a model pointed to by a foreign key in another | Pass |
#24537 - The order of fields in a model does not influence | Pass |
Tests autodetection of renamed models while simultaneously renaming one | Pass |
test_rename_referenced_primary_key (migrations.test_autodetector.AutodetectorTests) | Pass |
test_rename_related_field_preserved_db_column (migrations.test_autodetector.AutodetectorTests) | Pass |
test_renamed_referenced_m2m_model_case (migrations.test_autodetector.AutodetectorTests) | Pass |
#22300 - Adding an FK in the same "spot" as a deleted CharField should | Pass |
A migration with a FK between two models of the same app does | Pass |
#22275 - A migration with circular FK dependency does not try | Pass |
A migration with a FK between two models of the same app | Pass |
Setting order_with_respect_to adds a field. | Pass |
test_set_alter_order_with_respect_to_index_constraint_foo_together (migrations.test_autodetector.AutodetectorTests) | Pass |
test_supports_functools_partial (migrations.test_autodetector.AutodetectorTests) | Pass |
test_swappable (migrations.test_autodetector.AutodetectorTests) | Pass |
test_swappable_changed (migrations.test_autodetector.AutodetectorTests) | Pass |
test_swappable_circular_multi_mti (migrations.test_autodetector.AutodetectorTests) | Pass |
Swappable models get their CreateModel first. | Pass |
test_swappable_lowercase (migrations.test_autodetector.AutodetectorTests) | Pass |
Trim does not remove dependencies but does remove unwanted apps. | Pass |
The autodetector correctly deals with managed models. | Pass |
#23415 - The autodetector must correctly deal with custom FK on | Pass |
test_unmanaged_delete (migrations.test_autodetector.AutodetectorTests) | Pass |
test_unmanaged_to_managed (migrations.test_autodetector.AutodetectorTests) | Pass |
Loading...
Ridges.AI© 2025 Ridges AI. Building the future of decentralized AI development.
