Changes between Initial Version and Version 1 of Ticket #34647
- Timestamp:
- Jun 9, 2023, 6:48:56 AM (18 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #34647 – Description
initial v1 8 8 Looking 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. 9 9 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 10 Here's how to create the problem on a fresh project 13 11 14 12 {{{ … … 16 14 from django.contrib.auth.models import User 17 15 16 18 17 class ThisIsAModelWithALongName(models.Model): 19 """The Original Model"""20 18 name = models.CharField(max_length=128, blank=True) 21 19 archived_by = models.ForeignKey(User, null=True, blank=True, default=None, related_name="model_with_log_names", on_delete=models.SET_NULL) … … 26 24 }}} 27 25 26 {{{ python manage.py makemigrations && python manage.py migrate }}} 28 27 29 I then renamed the model 28 Step 2 - Rename the top class and the FK in the 2nd class but leave the related name in the first class 30 29 31 30 {{{ … … 33 32 from django.contrib.auth.models import User 34 33 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 model47 {{{48 from django.db import models49 from django.contrib.auth.models import User50 34 51 35 class 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"""59 36 name = models.CharField(max_length=128, blank=True) 60 37 archived_by = models.ForeignKey(User, null=True, blank=True, default=None, related_name="model_with_log_names", on_delete=models.SET_NULL) … … 63 40 class AnotherClassHere(models.Model): 64 41 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 44 Run migrations 45 46 Step 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 {{{ 49 from django.db import models 50 from django.contrib.auth.models import User 51 52 53 class 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 58 class 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 63 class 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) 65 65 long_name_property = models.ForeignKey(ThisIsAModelWithALongName, null=True, blank=True, default=None, related_name="another_class_here", on_delete=models.SET_NULL) 66 67 66 }}} 68 67 69 70 MySQL version was 8.0.33 using mysqlclient==2.1.1 68 run migrations, the error will occur and the DB will be left in a partially migrated state that can't be rolled back