#24536: dynamic default value that uses the orm cause an exception when running
migrate
-------------------------+-------------------------------------------------
Reporter: | Owner: nobody
Brandhor |
Type: Bug | Status: new
Component: | Version: 1.7
Migrations | Keywords: default migrate exception callable
Severity: Normal | dynamic
Triage Stage: | Has patch: 0
Unreviewed |
Easy pickings: 0 | UI/UX: 0
-------------------------+-------------------------------------------------
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
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24536>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/051.18e6eb71fbb40c7ae1ec59faf93eaf77%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.