Opened 5 months ago

Closed 5 months ago

Last modified 5 months ago

#35019 closed Bug (fixed)

Model with GeneratedField and ForeignKey raises an error on save().

Reported by: Deb Kumar Das Owned by: Sarah Boyce
Component: Database layer (models, ORM) Version: 5.0
Severity: Release blocker Keywords: GeneratedField save error
Cc: Lily Foote, Jeremy Nauta Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Deb Kumar Das)

When using GeneratedField column along with a ForeignKey column, getting error while saving.
The below model throws error while saving data from admin panel.

class Square(models.Model):
    side = models.IntegerField()
    area = models.GeneratedField(
        expression=F("side") * F("side"),
        output_field=models.BigIntegerField(),
        db_persist=True,
    )
    user = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)

Throws the below error

Cannot force an update in save() with no primary key.

attached error screenshot

Attachments (1)

django_error.PNG (32.7 KB ) - added by Deb Kumar Das 5 months ago.

Download all attachments as: .zip

Change History (12)

by Deb Kumar Das, 5 months ago

Attachment: django_error.PNG added

comment:1 by Deb Kumar Das, 5 months ago

Component: UncategorizedDatabase layer (models, ORM)

comment:2 by Deb Kumar Das, 5 months ago

Description: modified (diff)

comment:3 by Deb Kumar Das, 5 months ago

Description: modified (diff)

comment:4 by Mariusz Felisiak, 5 months ago

Cc: Lily Foote Jeremy Nauta added
Severity: NormalRelease blocker
Summary: GeneratedField save errorModel with GeneratedField and ForeignKey raises an error on save().
Triage Stage: UnreviewedAccepted

Good catch!

Regression in f333e3513e8bdf5ffeb6eeb63021c230082e6f95.

comment:5 by Sarah Boyce, 5 months ago

Has patch: set
Needs documentation: set
Owner: changed from nobody to Sarah Boyce
Status: newassigned

comment:6 by Simon Charette, 5 months ago

Needs tests: set
Patch needs improvement: set

The patch will need some improvements as we must consider that generated fields can also be deferred. We'll likely want to add tests for generated field deferral as well.

comment:7 by Sarah Boyce, 5 months ago

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

comment:8 by Mariusz Felisiak, 5 months ago

Triage Stage: AcceptedReady for checkin

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 5 months ago

Resolution: fixed
Status: assignedclosed

In b287af5d:

Fixed #35019 -- Fixed save() on models with both GeneratedFields and ForeignKeys.

Thanks Deb Kumar Das for the report.

Regression in f333e3513e8bdf5ffeb6eeb63021c230082e6f95.

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 5 months ago

In 03af8fb:

[5.0.x] Fixed #35019 -- Fixed save() on models with both GeneratedFields and ForeignKeys.

Thanks Deb Kumar Das for the report.

Regression in f333e3513e8bdf5ffeb6eeb63021c230082e6f95.

Backport of b287af5dc954628d4b336aefc5027b2edceee64b from main

comment:11 by Deb Kumar Das, 5 months ago

Thanks @Mariusz Felisiak and all the Django team, for a quick response and fix.

Note: See TracTickets for help on using tickets.
Back to Top