Opened 5 years ago

Last modified 5 years ago

#31186 closed Bug

Error updating unique field — at Version 1

Reported by: Kailegh Owned by: nobody
Component: Migrations Version: dev
Severity: Normal Keywords: migration, TextField, CharField
Cc: Markus Holtermann Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Mariusz Felisiak)

Hi, I think I have discovered a bug, if you have a field like the following:

audio_name = models.CharField(blank=False, max_length=255)

and decide to change it to the following one:

audio_name = models.TextField(blank=False)

it generates the following migration code:

   operations = [
        migrations.AlterModelOptions(
            name='s3_bucket_audios',
            options={},
        ),
        migrations.AlterField(
            model_name='s3_bucket_audios',
            name='audio_name',
            field=models.TextField(help_text='name of the audio in the s3 bucket'),
        ),
        migrations.AlterUniqueTogether(
            name='s3_bucket_audios',
            unique_together=set(),
        ),
    ]

However it that field was being use in a unique condition:

class Meta:
   ordering = [ "audio_name"]
   unique_together = ('audio_name')

you get the following error:

django.db.utils.OperationalError: (1170, "BLOB/TEXT column 'audio_name' used in key specification without a key length")

That is because it changes the field type before changing the unique condition, I think the order should be handled automatically, I had to change the code manually to:

   operations = [
        migrations.AlterModelOptions(
            name='s3_bucket_audios',
            options={},
        ),
        migrations.AlterUniqueTogether(
            name='s3_bucket_audios',
            unique_together=set(),
        ),
        migrations.AlterField(
            model_name='s3_bucket_audios',
            name='audio_name',
            field=models.TextField(help_text='name of the audio in the s3 bucket'),
        ),
    ]

It is not a big issue anyway...
Thanks a lot for your amazing work!!

Change History (1)

comment:1 by Mariusz Felisiak, 5 years ago

Description: modified (diff)
Type: UncategorizedBug
Note: See TracTickets for help on using tickets.
Back to Top