[Django] #34956: Migration should not be allowed for a non-deterministic GeneratedField

6 views
Skip to first unread message

Django

unread,
Nov 8, 2023, 4:44:45 AM11/8/23
to django-...@googlegroups.com
#34956: Migration should not be allowed for a non-deterministic GeneratedField
-----------------------------------------+------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 5.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
Hello hello
I didn't fully understand the GeneratedField and thought I would be able
to do something with comparisons against Now (which I ''now'' know I
cannot do).

{{{
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.

Django

unread,
Nov 8, 2023, 5:03:11 AM11/8/23
to django-...@googlegroups.com
#34956: Migration should not be allowed for a non-deterministic GeneratedField
-------------------------------------+-------------------------------------

Reporter: Sarah Boyce | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Django

unread,
Nov 8, 2023, 11:04:03 AM11/8/23
to django-...@googlegroups.com
#34956: Migration should not be allowed for a non-deterministic GeneratedField
-------------------------------------+-------------------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: | Status: closed
Cleanup/optimization |
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Reply all
Reply to author
Forward
0 new messages