{{{
from django.db import models
from django.db.models import Case, When, Value, Q
from django.db.models.functions import Now
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField("date published")
closed_date = models.DateTimeField("date closed", null=True)
status = models.GeneratedField(
expression=Case(
When(pub_date__lt=Now(), then=Value("unpublished")),
When(Q(closed_date__isnull=False, closed_date__gt=Now()),
then=Value("closed")),
default=Value("active"),
),
db_persist=False,
output_field=models.TextField(),
)
}}}
Django allowed me to make migrations and migrate here (I'm using SQLite)
but then when I tried to create a Question object it got very angry.
The traceback is very long but the main thing is:
{{{
OperationalError: non-deterministic use of strftime() in a generated
column
}}}
I learnt my lesson and won't do it again! But I'm wondering if we can
bring this error earlier and prevent the user from being able to
makemigrations/migrate? I can provide more details if needed
--
Ticket URL: <https://code.djangoproject.com/ticket/34956>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* status: new => closed
* resolution: => wontfix
* component: Uncategorized => Database layer (models, ORM)
* type: Uncategorized => Cleanup/optimization
Comment:
There are many functions that cannot be used of various databases, e.g.
`Random`, `SHA512`, `SHA384`, `SHA224`, `SHA1`, `MD5` cannot be used on
Oracle. As far as I'm, is not feasible for us to curate the list of
functions supported for each of database. This is also
[https://docs.djangoproject.com/en/dev/ref/models/fields/#generatedfield
documented] in `Database limitations`.
--
Ticket URL: <https://code.djangoproject.com/ticket/34956#comment:1>
Comment (by Simon Charette):
I know the exact same problem was discussed for indexes in the past (I
just can't find where). An idea that as brought up was to flag expressions
on whether they are deterministic or not but as Mariusz pointed out it's a
lot of work to get right.
--
Ticket URL: <https://code.djangoproject.com/ticket/34956#comment:2>