Re: [Django] #34984: Adding a field with default crashes for models with GeneratedField on SQLite.

19 views
Skip to first unread message

Django

unread,
Nov 21, 2023, 9:45:27 AM11/21/23
to django-...@googlegroups.com
#34984: Adding a field with default crashes for models with GeneratedField on
SQLite.
-------------------------------------+-------------------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by David Sanders):

Looks like felix already narrowed it down but here's a failing test case
just in case someone can use it:

{{{
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -5801,6 +5801,36 @@ class OperationTests(OperationTestBase):
def test_remove_generated_field_virtual(self):
self._test_remove_generated_field(db_persist=False)

+ @skipUnlessDBFeature("supports_stored_generated_columns")
+ def test_add_field_after_generated_field(self):
+ app_label = "test_adfagf"
+ project_state = self.set_up_test_model(app_label)
+ operation_1 = migrations.AddField(
+ "Pony",
+ "generated",
+ models.GeneratedField(
+ expression=Value(1),
+ output_field=models.IntegerField(),
+ db_persist=True,
+ ),
+ )
+ operation_2 = migrations.AddField(
+ "Pony",
+ "static",
+ models.IntegerField(default=2),
+ )
+ new_state = project_state.clone()
+ operation_1.state_forwards(app_label, new_state)
+ with connection.schema_editor() as editor:
+ operation_1.database_forwards(app_label, editor,
project_state, new_state)
+ project_state, new_state = new_state, new_state.clone()
+ operation_2.state_forwards(app_label, new_state)
+ with connection.schema_editor() as editor:
+ operation_2.database_forwards(app_label, editor,
project_state, new_state)
+ pony = new_state.apps.get_model(app_label,
"Pony").objects.create(weight=20)
+ self.assertEqual(pony.generated, 1)
+ self.assertEqual(pony.static, 2)
+

class SwappableOperationTests(OperationTestBase):
"""
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34984#comment:12>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 21, 2023, 9:54:56 AM11/21/23
to django-...@googlegroups.com
#34984: Adding a field with default crashes for models with GeneratedField on
SQLite.
-------------------------------------+-------------------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

I think it's enough to skip `GeneratedField`s when remaking tables on
SQLite:
{{{#!diff
diff --git a/django/db/backends/sqlite3/schema.py
b/django/db/backends/sqlite3/schema.py
index a8200a1a8c..713ef31437 100644
--- a/django/db/backends/sqlite3/schema.py
+++ b/django/db/backends/sqlite3/schema.py
@@ -106,7 +106,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
# its values must be already quoted.
mapping = {
f.column: self.quote_name(f.column)
- for f in model._meta.local_concrete_fields
+ for f in model._meta.local_concrete_fields if f.generated is
False
}
# This maps field names (not columns) for things like
unique_together
rename_mapping = {}

}}}
Sarah, would you like to prepare a patch?

--
Ticket URL: <https://code.djangoproject.com/ticket/34984#comment:13>

Django

unread,
Nov 21, 2023, 9:57:41 AM11/21/23
to django-...@googlegroups.com
#34984: Adding a field with default crashes for models with GeneratedField on
SQLite.
-------------------------------------+-------------------------------------
Reporter: Sarah Boyce | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Sarah Boyce):

I can tomorrow, if anyone wants to pick it up sooner that's also fine 🙂

--
Ticket URL: <https://code.djangoproject.com/ticket/34984#comment:14>

Django

unread,
Nov 21, 2023, 10:10:10 AM11/21/23
to django-...@googlegroups.com
#34984: Adding a field with default crashes for models with GeneratedField on
SQLite.
-------------------------------------+-------------------------------------
Reporter: Sarah Boyce | Owner: Sarah
| Boyce
Type: Bug | Status: assigned

Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: nobody => Sarah Boyce
* status: new => assigned


Comment:

Replying to [comment:14 Sarah Boyce]:


> I can tomorrow, if anyone wants to pick it up sooner that's also fine 🙂

No rush, we can wait until tomorrow.

--
Ticket URL: <https://code.djangoproject.com/ticket/34984#comment:15>

Django

unread,
Nov 22, 2023, 4:03:39 AM11/22/23
to django-...@googlegroups.com
#34984: Adding a field with default crashes for models with GeneratedField on
SQLite.
-------------------------------------+-------------------------------------
Reporter: Sarah Boyce | Owner: Sarah
| Boyce
Type: Bug | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/34984#comment:16>

Django

unread,
Nov 22, 2023, 6:30:34 AM11/22/23
to django-...@googlegroups.com
#34984: Adding a field with default crashes for models with GeneratedField on
SQLite.
-------------------------------------+-------------------------------------
Reporter: Sarah Boyce | Owner: Sarah
| Boyce
Type: Bug | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


Comment:

[https://github.com/django/django/pull/17505 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/34984#comment:17>

Django

unread,
Nov 22, 2023, 7:27:40 AM11/22/23
to django-...@googlegroups.com
#34984: Adding a field with default crashes for models with GeneratedField on
SQLite.
-------------------------------------+-------------------------------------
Reporter: Sarah Boyce | Owner: Sarah
| Boyce
Type: Bug | Status: closed

Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed

Keywords: sqlite | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"828082dad954e87d09a99b53424e6faa1860ccc7" 828082da]:
{{{
#!CommitTicketReference repository=""
revision="828082dad954e87d09a99b53424e6faa1860ccc7"
Fixed #34984 -- Skipped GeneratedFields when remaking tables on SQLite.

Regression in f333e3513e8bdf5ffeb6eeb63021c230082e6f95.t

Co-authored-by: Mariusz Felisiak <felisiak...@gmail.com>
Co-authored-by: David Sanders <shang.xia...@gmail.com>
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34984#comment:18>

Django

unread,
Nov 22, 2023, 7:28:00 AM11/22/23
to django-...@googlegroups.com
#34984: Adding a field with default crashes for models with GeneratedField on
SQLite.
-------------------------------------+-------------------------------------
Reporter: Sarah Boyce | Owner: Sarah
| Boyce
Type: Bug | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: sqlite | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"0c6ca522263b0f5c4075b3634d7e5507d9528b52" 0c6ca522]:
{{{
#!CommitTicketReference repository=""
revision="0c6ca522263b0f5c4075b3634d7e5507d9528b52"
[5.0.x] Fixed #34984 -- Skipped GeneratedFields when remaking tables on
SQLite.

Regression in f333e3513e8bdf5ffeb6eeb63021c230082e6f95.t

Co-authored-by: Mariusz Felisiak <felisiak...@gmail.com>
Co-authored-by: David Sanders <shang.xia...@gmail.com>

Backport of 828082dad954e87d09a99b53424e6faa1860ccc7 from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/34984#comment:19>

Reply all
Reply to author
Forward
0 new messages