Opened 5 weeks ago

Closed 4 weeks ago

Last modified 4 weeks ago

#35373 closed Bug (fixed)

GeneratedField alteration fail on SQLite due table remake data insertion

Reported by: Moshe Dicker Owned by: Simon Charette
Component: Database layer (models, ORM) Version: 5.0
Severity: Release blocker Keywords: migrations, db
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

A migration where I added an index to a generated column resulted in a migrations that fails with

django.db.utils.OperationalError: cannot INSERT into generated column "effectivePrice"

Change History (5)

comment:1 by Simon Charette, 5 weeks ago

Easy pickings: unset
Severity: NormalRelease blocker
Summary: db_index=True on a Generated Column generates failing migrationsGeneratedField alteration fail on SQLite due table remake data insertion
Triage Stage: UnreviewedAccepted

Thank you for your report, a traceback and a description of the backend you are using would have been appreciated.

This looks like a problem isolated to SQLite due to the way _remake_table doesn't exclude generated fields from its INSERT INTO new_table SELECT ... FROM old_table at table rename time (a necessary step since SQLite doesn't support most table alterations).

  • django/db/backends/sqlite3/schema.py

    diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py
    index 495714a894..32f4f08459 100644
    a b def is_self_referential(f):  
    150150            body.pop(old_field.name, None)
    151151            mapping.pop(old_field.column, None)
    152152            body[new_field.name] = new_field
    153             if old_field.null and not new_field.null:
     153            if new_field.generated:
     154                pass
     155            elif old_field.null and not new_field.null:
    154156                if new_field.db_default is NOT_PROVIDED:
    155157                    default = self.prepare_default(self.effective_default(new_field))
    156158                else:
Last edited 5 weeks ago by Simon Charette (previous) (diff)

comment:2 by Sarah Boyce, 4 weeks ago

Has patch: set
Owner: changed from nobody to Simon Charette
Status: newassigned

comment:3 by Sarah Boyce, 4 weeks ago

Triage Stage: AcceptedReady for checkin

comment:4 by Sarah Boyce <42296566+sarahboyce@…>, 4 weeks ago

Resolution: fixed
Status: assignedclosed

In d048f0d:

Fixed #35373 -- Fixed a crash when indexing a generated field on SQLite.

Generated fields have to be excluded from the INSERT query against the remade
table including the index.

Thanks Moshe Dicker for the report, David Sanders and Mariusz Felisiak for the
review.

comment:5 by Sarah Boyce <42296566+sarahboyce@…>, 4 weeks ago

In 9d79714:

[5.0.x] Fixed #35373 -- Fixed a crash when indexing a generated field on SQLite.

Generated fields have to be excluded from the INSERT query against the remade
table including the index.

Thanks Moshe Dicker for the report, David Sanders and Mariusz Felisiak for the
review.

Backport of d048f0d311cc9cb9578d687968f2d24a0a9efddb from main.

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