Changes between Initial Version and Version 1 of Ticket #34647


Ignore:
Timestamp:
Jun 9, 2023, 6:48:56 AM (18 months ago)
Author:
Steven Mapes
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #34647 – Description

    initial v1  
    88Looking at the output of sqlmigrate for the new migration and comparing the name of the foreign keys that Django generated I can see several that are going to clash as the indexes on the old table were not renamed when the table was renamed by the migration.
    99
    10 I did manage to re-create the issue this morning on both SQL Lite and MySQL backends on a new project,  but having sat down to create the bug report I am now struggling to have Django invoking migrations.RenameModel as it's just dropping and then creating the models, with all data, instead.
    11 
    12 I will have another go at the test project but it simply had two models
     10Here's how to create the problem on a fresh project
    1311
    1412{{{
     
    1614from django.contrib.auth.models import User
    1715
     16
    1817class ThisIsAModelWithALongName(models.Model):
    19     """The Original Model"""
    2018    name = models.CharField(max_length=128, blank=True)
    2119    archived_by = models.ForeignKey(User, null=True, blank=True, default=None, related_name="model_with_log_names", on_delete=models.SET_NULL)
     
    2624}}}
    2725
     26{{{ python manage.py makemigrations && python manage.py migrate }}}
    2827
    29 I then renamed the model
     28Step 2 - Rename the top class and the FK in the 2nd class but leave the related name in the first class
    3029
    3130{{{
     
    3332from django.contrib.auth.models import User
    3433
    35 class ThisIsAModelWithALongNameOld(models.Model):
    36     """The Original Model"""
    37     name = models.CharField(max_length=128, blank=True)
    38     archived_by = models.ForeignKey(User, null=True, blank=True, default=None, related_name="model_with_log_names_old", on_delete=models.SET_NULL)
    39 
    40 
    41 class AnotherClassHere(models.Model):
    42     long_name_property_old = models.ForeignKey(ThisIsAModelWithALongNameOld, null=True, blank=True, default=None, related_name="another_class_here", on_delete=models.SET_NULL)
    43 }}}
    44 
    45 
    46 Then I created the new model with the same name as the old model
    47 {{{
    48 from django.db import models
    49 from django.contrib.auth.models import User
    5034
    5135class ThisIsAModelWithALongNameOld(models.Model):
    52     """The Original Model"""
    53     name = models.CharField(max_length=128, blank=True)
    54     archived_by = models.ForeignKey(User, null=True, blank=True, default=None, related_name="model_with_log_names_old", on_delete=models.SET_NULL)
    55 
    56 
    57 class ThisIsAModelWithALongName(models.Model):
    58     """The NEW Model"""
    5936    name = models.CharField(max_length=128, blank=True)
    6037    archived_by = models.ForeignKey(User, null=True, blank=True, default=None, related_name="model_with_log_names", on_delete=models.SET_NULL)
     
    6340class AnotherClassHere(models.Model):
    6441    long_name_property_old = models.ForeignKey(ThisIsAModelWithALongNameOld, null=True, blank=True, default=None, related_name="another_class_here", on_delete=models.SET_NULL)
     42}}}
     43   
     44Run migrations
     45   
     46Step 3 - Add the new model back, add an FK into the AnotherClassHere table and rename the related_name on the first (old) model
     47 
     48{{{   
     49from django.db import models
     50from django.contrib.auth.models import User
     51
     52
     53class ThisIsAModelWithALongNameOld(models.Model):
     54    name = models.CharField(max_length=128, blank=True)
     55    archived_by = models.ForeignKey(User, null=True, blank=True, default=None, related_name="model_with_log_names_old", on_delete=models.SET_NULL)
     56
     57
     58class ThisIsAModelWithALongName(models.Model):
     59    name = models.CharField(max_length=128, blank=True)
     60    archived_by = models.ForeignKey(User, null=True, blank=True, default=None, related_name="model_with_log_names", on_delete=models.SET_NULL)
     61
     62   
     63class AnotherClassHere(models.Model):
     64    long_name_property_old = models.ForeignKey(ThisIsAModelWithALongNameOld, null=True, blank=True, default=None, related_name="another_class_here", on_delete=models.SET_NULL)
    6565    long_name_property = models.ForeignKey(ThisIsAModelWithALongName, null=True, blank=True, default=None, related_name="another_class_here", on_delete=models.SET_NULL)
    66 
    6766}}}
    6867
    69 
    70 MySQL version was 8.0.33 using mysqlclient==2.1.1
     68run migrations, the error will occur and the DB will be left in a partially migrated state that can't be rolled back
Back to Top