﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
35027	Error Creating a Model with a GeneratedField	Jason Christa	nobody	"
{{{
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.''
"	Bug	closed	Database layer (models, ORM)	5.0	Normal	duplicate			Unreviewed	0	0	0	0	0	0
