[Django] #35027: Error Creating a Model with a GeneratedField

8 views
Skip to first unread message

Django

unread,
Dec 11, 2023, 11:30:18 AM12/11/23
to django-...@googlegroups.com
#35027: Error Creating a Model with a GeneratedField
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
jasonchrista |
Type: Bug | Status: new
Component: Database | Version: 5.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
{{{
class ProjectHealth(models.Model):
class HEALTH(models.TextChoices):
GREEN = "green", "Green"
YELLOW = "yellow", "Yellow"
RED = "red", "Red"
NA = "not applicable", "N/A"


project = models.ForeignKey("Project", on_delete=models.CASCADE)
health = models.CharField(
choices=HEALTH.choices, max_length=50, default=HEALTH.GREEN,
db_default=HEALTH.GREEN
)
health_value = models.GeneratedField(
expression=Case(
When(health=HEALTH.GREEN, then=Value(1)),
When(health=HEALTH.YELLOW, then=Value(2)),
When(health=HEALTH.RED, then=Value(3)),
default=Value(0),
),
output_field=models.IntegerField(),
db_persist=True,
)
}}}

In this simple model when I try to create a new **ProjectHealth** record,
**health_value** is set as a deferred field in base save(). This causes
the rest of the save process to treat this as an update instead of an
insert. It ends with the error ''Cannot force an update in save() with no
primary key.''

--
Ticket URL: <https://code.djangoproject.com/ticket/35027>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 11, 2023, 11:49:19 AM12/11/23
to django-...@googlegroups.com
#35027: Error Creating a Model with a GeneratedField
-------------------------------------+-------------------------------------
Reporter: jasonchrista | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: duplicate
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 Simon Charette):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #35019, will be fixed in Django 5.0.1 expected to be released
on Jan 2nd.

--
Ticket URL: <https://code.djangoproject.com/ticket/35027#comment:1>

Reply all
Reply to author
Forward
0 new messages