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

-- 
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018bae51d907-f86d314d-d134-4c7d-b0eb-6f14e8bf1e1e-000000%40eu-central-1.amazonses.com.

Reply via email to