Opened 2 weeks ago

Last modified 2 weeks ago

#28980 new Cleanup/optimization

Make the autodetector validate the type of one-off default values

Reported by: Nikos Katsos Owned by: nobody
Component: Migrations Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

As the title says Django doesn't check the type of one-off value. Recently I had a field like this

    start_time = models.DateTimeField(editable=False)

1) I gave to the field an one-off value

>>> 0 

2) then the migration file had those operations

    operations = [
        migrations.AddField(
            model_name='x',
            name='outcome',
            field=models.CharField(default='TO', max_length=16),
            preserve_default=False,
        ),
        migrations.AddField(
            model_name='x',
            name='start_time',
            field=models.DateTimeField(default=0 editable=False),
            preserve_default=False,
        ),
    ]

when I found my error i couldn't really do anything
I changed

            field=models.DateTimeField(default=0, editable=False),

to

            field=models.DateTimeField(default=timezone.now, editable=False),

so after updating my migration file, I got an error "django.db.utils.OperationalError: (1060, "Duplicate column name "outcome")".
I could neither re-migrate because the operations isn't a bunch of queries in transaction, nor create a new migration.
Only reverting to zero and apply all migrations again worked.

This has to have a workaround.

Change History (1)

comment:1 Changed 2 weeks ago by Tim Graham

Summary: Django doesn't check the type of one-off valueMake the autodetector validate the type of one-off default values
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization
Note: See TracTickets for help on using tickets.
Back to Top