Opened 9 months ago

Closed 9 months ago

Last modified 9 months ago

#35350 closed Bug (fixed)

Crash on write of new instance when using GeneratedField

Reported by: Matt Hegarty Owned by: Sarah Boyce
Component: Database layer (models, ORM) Version: 5.0
Severity: Release blocker Keywords: GeneratedField
Cc: 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 Matt Hegarty)

We have an issue in django-import-export where imports of new instances fails when the model has a GeneratedField.

This occurs when an instance is being created which doesn't already exist in the database, which the docs imply is supported by GeneratedField:

A stored generated column is computed when it is written (inserted or updated)

The stack trace is attached.

Steps to reproduce:

  1. Clone this repo and use the 'issue-1779-generatedfield' branch.
  2. Create a venv
  3. Install the example app following the instructions here
  4. Navigate to "Books", click "Import", then select the attached sample.csv file.
  5. Click "Submit" and you will see the error.
  • python 3.12
  • django 5.0.3

Attachments (2)

sample.csv (50 bytes ) - added by Matt Hegarty 9 months ago.
sample csv for import
st1.txt (1.8 KB ) - added by Matt Hegarty 9 months ago.
stack trace

Download all attachments as: .zip

Change History (9)

by Matt Hegarty, 9 months ago

Attachment: sample.csv added

sample csv for import

by Matt Hegarty, 9 months ago

Attachment: st1.txt added

stack trace

comment:1 by Matt Hegarty, 9 months ago

Description: modified (diff)

comment:2 by Simon Charette, 9 months ago

This is similar to #35019 (fixed by 03af8fbd0f1cfc08edadda248cf5d3498e8ed2f7) but when dealing with instances that have a primary key assigned and force_insert is not specified.

When this is the case Mode.save will first attempt to perform an UPDATE as it cannot know whether or not the row exists.

It seems that the non_pk logic in _save_table needs to be adapted to also needs to exclude deferred fields when computing values for update purpose.

Last edited 9 months ago by Simon Charette (previous) (diff)

comment:3 by Simon Charette, 9 months ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

comment:4 by Sarah Boyce, 9 months ago

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

comment:5 by Natalia Bidart, 9 months ago

Triage Stage: AcceptedReady for checkin

comment:6 by Sarah Boyce <42296566+sarahboyce@…>, 9 months ago

Resolution: fixed
Status: assignedclosed

In 8b53560e:

Fixed #35350 -- Fixed save() with pk set on models with GeneratedFields.

Thanks Matt Hegarty for the report and Simon Charette and Natalia Bidart for the reviews.

Regression in f333e35.

comment:7 by Sarah Boyce <42296566+sarahboyce@…>, 9 months ago

In f075a19e:

[5.0.x] Fixed #35350 -- Fixed save() with pk set on models with GeneratedFields.

Thanks Matt Hegarty for the report and Simon Charette and Natalia Bidart for the reviews.

Regression in f333e35.

Backport of 8b53560eea9f10a1271d3bdf765dc6f969c7d9d5 from main.

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