Opened 5 years ago
Last modified 5 years ago
#31665 closed Bug
Auto-migrations fail on postgres when Charfield's default value is longer than old constraint — at Version 3
Reported by: | shadytradesman | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 2.2 |
Severity: | Normal | Keywords: | charfield resize default alterfield max_length |
Cc: | 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 )
Migration to create the field
# Generated by Django 2.2.12 on 2020-05-29 19:06 import cells.models from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('cells', '0003_auto_20200529_1832'), ] operations = [ migrations.AddField( model_name='cell', name='invite_link_secret_key', field=models.CharField(default=cells.models.random_string, max_length=7), ), ]
Migration to resize the field:
# Generated by Django 2.2.12 on 2020-05-29 23:56 import cells.models from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ ('cells', '0004_auto_20200529_2006'), ] operations = [ migrations.AlterField( model_name='cell', name='invite_link_secret_key', field=models.CharField(default=cells.models.random_string, max_length=64), ), ]
Here is the random string method:
def random_string(): return hashlib.sha224(bytes(random.randint(1, 99999999))).hexdigest()
This migration worked on mysql when I was developing locally, but it was because I updated the random_string() function to provide a string that was longer after I ran the migration. It failed when I ran it against my prod Postgres 9.5.15 database.
I suggest including default value functions in the migrations so that when old migrations are run against new code, you don't have issues.
Change History (3)
comment:1 by , 5 years ago
Description: | modified (diff) |
---|
comment:2 by , 5 years ago
Description: | modified (diff) |
---|---|
Summary: | Auto-migrations fail on postgres when resizing Charfield and setting default value longer than old constraint → Auto-migrations fail on postgres when Charfield's default value is longer than old constraint |
comment:3 by , 5 years ago
Description: | modified (diff) |
---|