Opened 10 years ago

Closed 10 years ago

#24536 closed Bug (duplicate)

dynamic default value that uses the orm cause an exception when running migrate

Reported by: Brandon Owned by: nobody
Component: Migrations Version: 1.7
Severity: Normal Keywords: default migrate exception callable dynamic
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

for example

def calc_number():
    a = A.objects.order_by("number").last()
    if a:
        return a.number+1
    return 1

class A(models.Model):
    number = models.IntegerField(default=calc_number)

when running migrate calc_number() is called as well and since that table doesn't exist yet in the db it raises a django.db.utils.ProgrammingError
I've tried to catch the exception inside calc_number but it raises another exception

django.db.utils.InternalError: current transaction is aborted, commands ignored until end of transaction block

the only workaround I've found is to use traceback to check if one of the function called is "migrate"

import traceback
for s in traceback.extract_stack():
    if s[2] == "migrate":
        return 1

Change History (2)

comment:1 by Paul Hallett, 10 years ago

Needs tests: set

comment:2 by Tim Graham, 10 years ago

Resolution: duplicate
Status: newclosed

Duplicate of #24182

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