Changes between Initial Version and Version 1 of Ticket #28987, comment 9


Ignore:
Timestamp:
Nov 8, 2022, 7:35:17 AM (23 months ago)
Author:
Bhuvnesh

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #28987, comment 9

    initial v1  
    11It is also failing for the case if process is reversed, like when we migrate passing self/Foo to the m2m field and then change it to Bar.(due to the same reason that we are not altering {{{m2m_fields}}})
     2
     3I tried to alter self pointing field and with the changes below its working as expected for the above issue and passing all the tests as well.
     4
     5{{{#!diff
     6diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py
     7index 88fa466f79..970820827e 100644
     8--- a/django/db/backends/sqlite3/schema.py
     9+++ b/django/db/backends/sqlite3/schema.py
     10@@ -174,7 +174,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
     11             super().alter_field(model, old_field, new_field, strict=strict)
     12 
     13     def _remake_table(
     14-        self, model, create_field=None, delete_field=None, alter_field=None
     15+        self, model, create_field=None, delete_field=None, alter_field=None, alter_self_field=None
     16     ):
     17         """
     18         Shortcut to transform a model from old_model into new_model
     19@@ -236,6 +236,16 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
     20                 mapping[create_field.column] = self.prepare_default(
     21                     self.effective_default(create_field),
     22                 )
     23+
     24+        # Alter field pointing to the model itself.
     25+        if alter_self_field:
     26+            old_self_field, new_self_field = alter_self_field
     27+            body.pop(old_self_field.name, None)
     28+            mapping.pop(old_self_field.column, None)
     29+            body[new_self_field.name] = new_self_field
     30+            mapping[new_self_field.column] = self.quote_name(old_self_field.column)
     31+            rename_mapping[old_self_field.name] = new_self_field.name
     32+
     33         # Add in any altered fields
     34         if alter_field:
     35             old_field, new_field = alter_field
     36@@ -507,6 +517,14 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
     37                         new_field.m2m_reverse_field_name()
     38                     ),
     39                 ),
     40+                alter_self_field=(
     41+                    old_field.remote_field.through._meta.get_field(
     42+                        old_field.m2m_field_name()
     43+                    ),
     44+                    new_field.remote_field.through._meta.get_field(
     45+                        new_field.m2m_field_name()
     46+                    ),
     47+                ),
     48             )
     49             return
     50}}}
Back to Top