Opened 4 years ago

Closed 4 years ago

#26609 closed Bug (wontfix)

Spurious migrations caused by non-deteministic field choices order

Reported by: Harry Percival Owned by: nobody
Component: Database layer (models, ORM) Version: 1.9
Severity: Normal Keywords: migrations
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Not sure if this is *quite* a bug, but thought I'd mention it since it was a puzzler.

I had an app that seemed to be generatic infinite migrations, ie "makemigrations" would always create new migrations even if I didn't do anything about it.

Eventually I figured out it was (I think) due to Python's non-deterministic dict ordering behaviour (this may be Python 3 only, IIRC said nondetermininstic ordering is to avoid some sort of security thing).

code, eg:

THINGS = {
    'key1': 'val1',
    'key2': 'val2',
    #etc
}

# ...

    myfield = models.CharField(choices=[(k,v) for k,v in THINGS], max_length=255)

I fixed it by using "sorted" on my list comprehension, but I imagine other people might be v confused, and it feels like something ppl are quite likely to do (use a dict for the basis of choices in a field?)

Change History (3)

comment:1 Changed 4 years ago by Harry Percival

Summary: Spurious migrations cause by non-deteministic field choices orderSpurious migrations caused by non-deteministic field choices order

comment:3 Changed 4 years ago by Simon Charette

Resolution: wontfix
Status: newclosed

I'm afraid there's not much we can do about it.

The choices option is meant to be ordered and the migration framework correctly detects changes in ordering.

Note: See TracTickets for help on using tickets.
Back to Top