#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.
--
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 view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107018c59b6fd72-d1d25a78-f194-4823-b511-cabf0d29a310-000000%40eu-central-1.amazonses.com.