Hi Django developers,

I'm new to the community and I'm not sure if this is the right place to ask 
my question, so please let me know if there's a more appropriate channel 
for this type of discussion.

I am currently working on ticket #24686 and I need help. I have figured out 
what I think is the best way to move a model from one app to another in 
Django. This involves applying four separate operations in four different 
migrations. The operations are:

   1. 
   
   Rename table operation: rename the physical table name on the database
   1.1. This operation should be a SeparateDatabaseAndState operation and 
   only a database operation, and do not change any state.
   2. 
   
   Create table operation: Move the model code to the new app, and create a 
   CreateModel only in state, with SeparateDatabaseAndState operation.
   3. 
   
   Update all foreign keys that points to the moved model
   4. 
   
   Delete the old model table, but only in state, using 
   SeparateDatabaseAndState operation.
   
I have implemented this logic in a new method on the MigrationAutodetector 
and called it "generate_moved_models". 
<https://github.com/durvalcarvalho/django/blob/b8a5155f474de8dadf92495d1ec9c668860aeb27/django/db/migrations/autodetector.py#L616>
 
However, I am having trouble defining these operations as dependent on each 
other so that they are written in separate migrations. 

I am confused about how dependencies tuple works and how  check_dependency 
method handle SeparateDatabaseAndState operations.

For example, the dependency logic of the 4 operations looked like this:

* The first operation (AlterModelTable to rename the table name on the 
database) only depends on the previous table creation.
    * [ (old_app_label, moved_model_name, None, True) ]

* The second operation (the state creation of the model on the new app) 
depends of the The first operation (and the table creation)
    [ (old_app_label, moved_model_name, None, True), 
      (old_app_label, moved_model_name, None, "alter") ]


* The third operation (the foreign keys updates) will depend of the two 
previous operations.
    [ (old_app_label, moved_model_name, None, True),
      (old_app_label, moved_model_name, None, "alter"),
      (new_app_label, moved_model_name, None, True)]

* Finally, the fourth operation will depend on the three previous 
operations, and the third operation may have generated several dependencies 
depending on the number of foreign keys involving the moved table.
    [ (old_app_label, moved_model_name, None, True),
      (old_app_label, moved_model_name, None, "alter"),
      (new_app_label, moved_model_name, None, True),
      (app_label_1, model_name_10, field_name_15, "alter"),
      (app_label_2, model_name_20, field_name_4, "alter")]


If we apply this dependency logic, we can consider a scenario where there 
are initially only two models, such as the Tires and Cars models mentioned 
by João. In this situation, the migration process will generate two 
migration files. The file in the app where the model was moved will contain 
the State CreateModel operation. The remaining migration files will be 
located in the old app. And the strangest thing is that these two 
migrations depend on each other, which prevents them from being executed 
with the migrate command.

I would appreciate any feedback on this approach and how to define the 
dependencies correctly.

Thank you all in advance for your help.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/31f898ae-2490-4f72-8526-ee1de3fa8e71n%40googlegroups.com.

Reply via email to