﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24536	dynamic default value that uses the orm cause an exception when running migrate	Brandon	nobody	"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
}}}
"	Bug	closed	Migrations	1.7	Normal	duplicate	default migrate exception callable dynamic		Unreviewed	0	0	1	0	0	0
