# ORM

**URL:** https://forum.djangoproject.com/c/internals/orm/9.md

[Latest](https://forum.djangoproject.com/latest.md) · [Categories](https://forum.djangoproject.com/categories.md) · [Tags](https://forum.djangoproject.com/tags.md)

---

## [About the ORM category](https://forum.djangoproject.com/t/about-the-orm-category/20)

<div class="topic-metadata">

**Author:** [@andrewgodwin](https://forum.djangoproject.com/u/andrewgodwin)\
**Replies:** 0\
**Last updated:** [September 9, 2019, 1:36am UTC](https://forum.djangoproject.com/t/about-the-orm-category/20 "2019-09-09T01:36:24Z")

</div>

Discussion of the Django ORM, migrations and database backends.

---

## [sqlparse is no longer maintained – implications for Django](https://forum.djangoproject.com/t/sqlparse-is-no-longer-maintained-implications-for-django/46346)

<div class="topic-metadata">

**Author:** [@andialbrecht](https://forum.djangoproject.com/u/andialbrecht)\
**Replies:** 1\
**Last updated:** [November 2, 2026, 3:05pm UTC](https://forum.djangoproject.com/t/sqlparse-is-no-longer-maintained-implications-for-django/46346 "2026-11-02T15:05:57Z")

</div>

Hi everyone, I’m the author of sqlparse, which Django has been using for many years. Today I’ve released version 0.6.1, and that will be the last release. There won’t be any further updates, including security fixes. F…

---

## [Proposal: Leverage Oracle Test Pilot for Django CI](https://forum.djangoproject.com/t/proposal-leverage-oracle-test-pilot-for-django-ci/45197)

<div class="topic-metadata">

**Author:** [@loiclefevre](https://forum.djangoproject.com/u/loiclefevre)\
**Replies:** 21\
**Last updated:** [October 28, 2026, 10:00pm UTC](https://forum.djangoproject.com/t/proposal-leverage-oracle-test-pilot-for-django-ci/45197 "2026-10-28T22:00:24Z")

</div>

Hello, I work at Oracle and I’m leading the Oracle Test Pilot for Third-Party Software program. During the last 3 years, we’ve been building Oracle Test Pilot to provide access to the latest versions of the Oracle data…

---

## [JSON persistence for Oracle](https://forum.djangoproject.com/t/json-persistence-for-oracle/46282)

<div class="topic-metadata">

**Author:** [@loiclefevre](https://forum.djangoproject.com/u/loiclefevre)\
**Replies:** 1\
**Last updated:** [October 27, 2026, 4:39pm UTC](https://forum.djangoproject.com/t/json-persistence-for-oracle/46282 "2026-10-27T16:39:36Z")

</div>

Hi, I wanted to bring some clarifications to the evolution of JSON persistence inside Oracle. That would probably lead to lower storage footprint, better performance and increased support for related capabilities. A/ P…

---

## [Group by, grouping sets, rollup & cube](https://forum.djangoproject.com/t/group-by-grouping-sets-rollup-cube/22198)

<div class="topic-metadata">

**Author:** [@shangxiao](https://forum.djangoproject.com/u/shangxiao)\
**Replies:** 1\
**Last updated:** [October 27, 2026, 1:11pm UTC](https://forum.djangoproject.com/t/group-by-grouping-sets-rollup-cube/22198 "2026-10-27T13:11:42Z")

</div>

Hi folks (in particular Adam, Simon & Mariusz) There’s a ticket & (closed) PR for adding support for GROUPING SETS, ROLLUP & CUBE by adding a method group\_by() to querysets. Is this something we can progress? I needed…

---

## [New model instances with a primary key: what should Django allow? (#36259)](https://forum.djangoproject.com/t/new-model-instances-with-a-primary-key-what-should-django-allow-36259/46262)

<div class="topic-metadata">

**Author:** [@alin-morosanu](https://forum.djangoproject.com/u/alin-morosanu)\
**Replies:** 4\
**Last updated:** [October 26, 2026, 4:05am UTC](https://forum.djangoproject.com/t/new-model-instances-with-a-primary-key-what-should-django-allow-36259/46262 "2026-10-26T04:05:33Z")

</div>

Hi, I’m working on ticket 36259 with Djangonaut Space. I’d like your view on the intended behavior before I go further. The ticket lists four cases where a new model instance (built in Python, not loaded from the databa…

---

## [How to identify the offending field in \`django.db.utils.DataError: value too long for type character varying(...)\`](https://forum.djangoproject.com/t/how-to-identify-the-offending-field-in-django-db-utils-dataerror-value-too-long-for-type-character-varying/46186)

<div class="topic-metadata">

**Author:** [@VBobCat](https://forum.djangoproject.com/u/VBobCat)\
**Replies:** 3\
**Last updated:** [October 19, 2026, 10:59pm UTC](https://forum.djangoproject.com/t/how-to-identify-the-offending-field-in-django-db-utils-dataerror-value-too-long-for-type-character-varying/46186 "2026-10-19T22:59:46Z")

</div>

When the error is in the log, but the data that caused it is no longer available, sometimes there’s no way of telling which field was the one that couldn’t store the longer string (when there are several fields with the …

---

## [Deferring the per-instance related manager and QuerySet in prefetch\_related()](https://forum.djangoproject.com/t/deferring-the-per-instance-related-manager-and-queryset-in-prefetch-related/46178)

<div class="topic-metadata">

**Author:** [@kedmenec](https://forum.djangoproject.com/u/kedmenec)\
**Replies:** 2\
**Last updated:** [October 12, 2026, 7:28pm UTC](https://forum.djangoproject.com/t/deferring-the-per-instance-related-manager-and-queryset-in-prefetch-related/46178 "2026-10-12T19:28:27Z")

</div>

Hi all, I’d like feedback on a prefetch\_related() optimisation before opening a ticket. We run a Django + DRF API with many list endpoints whose serializers read several prefetched to-many relations per row. Profiling …

---

## [Let's talk zero-downtime migrations](https://forum.djangoproject.com/t/lets-talk-zero-downtime-migrations/28388)

<div class="topic-metadata">

**Author:** [@pwtail](https://forum.djangoproject.com/u/pwtail)\
**Replies:** 11\
**Last updated:** [October 9, 2024, 11:47am UTC](https://forum.djangoproject.com/t/lets-talk-zero-downtime-migrations/28388 "2024-10-09T11:47:28Z")

</div>

Hi there, djangonauts! I have a simple idea in mind that I want to discuss. As a preface would like to say that, in my opinion, zero-downtime migrations are almost impossible to support in universal way. For any realist…

---

## [Supporting aggregate expressions in QuerySet.update()](https://forum.djangoproject.com/t/supporting-aggregate-expressions-in-queryset-update/46053)

<div class="topic-metadata">

**Author:** [@jadaun-abhay](https://forum.djangoproject.com/u/jadaun-abhay)\
**Replies:** 6\
**Last updated:** [September 20, 2026, 3:07pm UTC](https://forum.djangoproject.com/t/supporting-aggregate-expressions-in-queryset-update/46053 "2026-09-20T15:07:49Z")

</div>

Hi all, I’d like to start a discussion about allowing aggregate expressions over related objects in QuerySet.update(). The problem Denormalized counters and totals are a common pattern, and refreshing them in bulk cur…

---

## [select\_related removed when performing a deletion](https://forum.djangoproject.com/t/select-related-removed-when-performing-a-deletion/40857)

<div class="topic-metadata">

**Author:** [@PaulGilmartin](https://forum.djangoproject.com/u/PaulGilmartin)\
**Replies:** 5\
**Last updated:** [September 17, 2026, 10:07am UTC](https://forum.djangoproject.com/t/select-related-removed-when-performing-a-deletion/40857 "2026-09-17T10:07:49Z")

</div>

Today whilst investigating an n+1 query issue, we noticed that the following code is present in the django/db/models/query.py: class QuerySet(AltersData): ... def delete(self): """Delete the records in the c…

---

## [\#31255 — is the ProjectState-in-reduce() approach still the intended direction?](https://forum.djangoproject.com/t/31255-is-the-projectstate-in-reduce-approach-still-the-intended-direction/45932)

<div class="topic-metadata">

**Author:** [@0770-Lakshya](https://forum.djangoproject.com/u/0770-Lakshya)\
**Replies:** 0\
**Last updated:** [September 5, 2026, 11:26am UTC](https://forum.djangoproject.com/t/31255-is-the-projectstate-in-reduce-approach-still-the-intended-direction/45932 "2026-09-05T11:26:52Z")

</div>

Hi all, I’m Lakshya Soni, a 2nd year student at IIT Bhilai, new to contributing to Django. I’ve set up the development environment, worked through the contributing tutorial, and have been reading the migrations code loo…

---

## [PostgreSQL: compile \_\_in lookup to "= ANY(%s)" to avoid O(N) placeholder rewrite](https://forum.djangoproject.com/t/postgresql-compile-in-lookup-to-any-s-to-avoid-o-n-placeholder-rewrite/45386)

<div class="topic-metadata">

**Author:** [@yeung108](https://forum.djangoproject.com/u/yeung108)\
**Replies:** 5\
**Last updated:** [July 29, 2026, 7:36pm UTC](https://forum.djangoproject.com/t/postgresql-compile-in-lookup-to-any-s-to-avoid-o-n-placeholder-rewrite/45386 "2026-07-29T19:36:06Z")

</div>

Hi all, would like to raise a problem and see if it echos with everyone else. Problem Django’s In lookup currently compiles to WHERE col IN (%s, %s, …, %s) with N placeholders, on every backend including PostgreSQL. For…

---

## [Implementing a table-valued function](https://forum.djangoproject.com/t/implementing-a-table-valued-function/45551)

<div class="topic-metadata">

**Author:** [@vtbassmatt](https://forum.djangoproject.com/u/vtbassmatt)\
**Replies:** 3\
**Last updated:** [July 29, 2026, 1:13am UTC](https://forum.djangoproject.com/t/implementing-a-table-valued-function/45551 "2026-07-29T01:13:18Z")

</div>

tl;dr can anyone point me towards how I can ask the ORM to spit out just a table’s name, not table.column, in a SQL expression? I’ve built some infrastructure around SQLite’s FTS5 system for a small, personal app. (I tr…

---

## [Feedback wanted: pluggable migration recorder (MIGRATION\_RECORDER setting)](https://forum.djangoproject.com/t/feedback-wanted-pluggable-migration-recorder-migration-recorder-setting/45329)

<div class="topic-metadata">

**Author:** [@Muazzam741](https://forum.djangoproject.com/u/Muazzam741)\
**Replies:** 0\
**Last updated:** [June 21, 2026, 4:58pm UTC](https://forum.djangoproject.com/t/feedback-wanted-pluggable-migration-recorder-migration-recorder-setting/45329 "2026-06-21T16:58:26Z")

</div>

Hi all, I’ve opened a new-feature idea and would love feedback before it goes any further: :backhand\_index\_pointing\_right: Make the migration recorder pluggable via a MIGRATION\_RECORDER setting · Issue #180 · django/new…

---

## [Should literal db\_default values skip model validation?](https://forum.djangoproject.com/t/should-literal-db-default-values-skip-model-validation/45322)

<div class="topic-metadata">

**Author:** [@jacobtylerwalls](https://forum.djangoproject.com/u/jacobtylerwalls)\
**Replies:** 1\
**Last updated:** [June 18, 2026, 9:23pm UTC](https://forum.djangoproject.com/t/should-literal-db-default-values-skip-model-validation/45322 "2026-06-18T21:23:09Z")

</div>

We skipped model validation of db\_default values to fix a crash in 5.0.4 (PR). For generated values, that makes sense, and aligns with GeneratedField. But for literal values, I find that surprising. Can’t we resolve th…

---

## [Proposal: Add generate\_series() support to contrib.postgres?](https://forum.djangoproject.com/t/proposal-add-generate-series-support-to-contrib-postgres/21947)

<div class="topic-metadata">

**Author:** [@shangxiao](https://forum.djangoproject.com/u/shangxiao)\
**Replies:** 7\
**Last updated:** [May 22, 2026, 3:09am UTC](https://forum.djangoproject.com/t/proposal-add-generate-series-support-to-contrib-postgres/21947 "2026-05-22T03:09:12Z")

</div>

Hi folks, This one’s a long shot but thought I might as well throw the idea out there: I often make use of Postgres’ generate\_series() to generate reports aggregating data against a date range in buckets of intervals l…

---

## [Ticket #36489 follow up: get\_or\_create()/update\_or\_create() calls on a OneToOneField](https://forum.djangoproject.com/t/ticket-36489-follow-up-get-or-create-update-or-create-calls-on-a-onetoonefield/45060)

<div class="topic-metadata">

**Author:** [@sinachaichi](https://forum.djangoproject.com/u/sinachaichi)\
**Replies:** 2\
**Last updated:** [May 8, 2026, 6:35pm UTC](https://forum.djangoproject.com/t/ticket-36489-follow-up-get-or-create-update-or-create-calls-on-a-onetoonefield/45060 "2026-05-08T18:35:12Z")

</div>

Hi everyone, I’m looking for advice on how to follow up on a PR that hasn’t received a review yet. I submitted a fix for ticket #36489 back in September (a race condition bug where concurrent get\_or\_create()/update\_or\_…

---

## [Dynamically populate constraints in Field.contribute\_to\_class](https://forum.djangoproject.com/t/dynamically-populate-constraints-in-field-contribute-to-class/22934)

<div class="topic-metadata">

**Author:** [@olivierdalang](https://forum.djangoproject.com/u/olivierdalang)\
**Replies:** 25\
**Last updated:** [April 24, 2026, 1:50pm UTC](https://forum.djangoproject.com/t/dynamically-populate-constraints-in-field-contribute-to-class/22934 "2026-04-24T13:50:37Z")

</div>

Hey ! I’m writing a custom model field and would like it to come with a database CHECK constraint. The actual use case is DB validation of Django’s choices, to avoid invalid values (since the data can be edited from out…

---

## [Extract() isn't safe to use in db\_default on SQLite if TIME\_ZONE changes at some point](https://forum.djangoproject.com/t/extract-isnt-safe-to-use-in-db-default-on-sqlite-if-time-zone-changes-at-some-point/44781)

<div class="topic-metadata">

**Author:** [@jacobtylerwalls](https://forum.djangoproject.com/u/jacobtylerwalls)\
**Replies:** 7\
**Last updated:** [March 31, 2026, 1:03pm UTC](https://forum.djangoproject.com/t/extract-isnt-safe-to-use-in-db-default-on-sqlite-if-time-zone-changes-at-some-point/44781 "2026-03-31T13:03:13Z")

</div>

If: a field’s database default uses Extract() UZE\_TZ = True TIME\_ZONE is something other than UTC … then the timezone baked into the database default will differ from the timezone used by Django to generate values. F…

---

## [Improve validation for related fields in ModelAdmin ordering](https://forum.djangoproject.com/t/improve-validation-for-related-fields-in-modeladmin-ordering/44614)

<div class="topic-metadata">

**Author:** [@KhadyotTakale](https://forum.djangoproject.com/u/KhadyotTakale)\
**Replies:** 4\
**Last updated:** [March 27, 2026, 1:33pm UTC](https://forum.djangoproject.com/t/improve-validation-for-related-fields-in-modeladmin-ordering/44614 "2026-03-27T13:33:47Z")

</div>

Hello everyone, I have noticed an issue with how Django validates ordering in the admin. The Problem If we use a related field in ordering, like → ordering = \["author\_\_name"\] Django does not validate it during mana…

---

## [Proposal: Lightweight N+1 Query Detection for Django ORM (DEBUG-only diagnostic)](https://forum.djangoproject.com/t/proposal-lightweight-n-1-query-detection-for-django-orm-debug-only-diagnostic/44433)

<div class="topic-metadata">

**Author:** [@logi](https://forum.djangoproject.com/u/logi)\
**Replies:** 4\
**Last updated:** [March 21, 2026, 7:15pm UTC](https://forum.djangoproject.com/t/proposal-lightweight-n-1-query-detection-for-django-orm-debug-only-diagnostic/44433 "2026-03-21T19:15:57Z")

</div>

Hi everyone, I’d like to get some thoughts on a small idea for improving developer feedback around N+1 queries in Django applications. As most of us know, N+1 queries are one of the most common performance issues when …

---

## [Faster bulk\_create using dictionaries](https://forum.djangoproject.com/t/faster-bulk-create-using-dictionaries/43891)

<div class="topic-metadata">

**Author:** [@adamsol](https://forum.djangoproject.com/u/adamsol)\
**Replies:** 8\
**Last updated:** [March 21, 2026, 11:34am UTC](https://forum.djangoproject.com/t/faster-bulk-create-using-dictionaries/43891 "2026-03-21T11:34:43Z")

</div>

Recently, there were some optimizations in bulk\_create: #35936 (Speeding up Postgres bulk\_create by using unnest) – Django #36088 (Avoid unnecessary DEFAULT usage on bulk\_create for models with db\_default fields) – Dja…

---

## [ExtractYear transform indexing](https://forum.djangoproject.com/t/extractyear-transform-indexing/44435)

<div class="topic-metadata">

**Author:** [@charettes](https://forum.djangoproject.com/u/charettes)\
**Replies:** 1\
**Last updated:** [March 16, 2026, 6:09am UTC](https://forum.djangoproject.com/t/extractyear-transform-indexing/44435 "2026-03-16T06:09:07Z")

</div>

To make sure no one jumps the gun on trying to “solve” this problem I’ll start this thread by stating that I don’t expect any actions to be taken as part of this discussion but I figured it was worth sharing this finding…

---

## [(private?) API change for subclassed fields](https://forum.djangoproject.com/t/private-api-change-for-subclassed-fields/44418)

<div class="topic-metadata">

**Author:** [@shaib](https://forum.djangoproject.com/u/shaib)\
**Replies:** 5\
**Last updated:** [March 9, 2026, 9:34pm UTC](https://forum.djangoproject.com/t/private-api-change-for-subclassed-fields/44418 "2026-03-09T21:34:31Z")

</div>

Hi, One of the highlighted new features for Django 6.1 is Database-level delete options for ForeignKey.on\_delete. It is indeed an important feature. But as part of its implementation, ForeignKey’s check() method now pas…

---

## [Deterministic Migration Replay & Schema Drift Detection for Django](https://forum.djangoproject.com/t/deterministic-migration-replay-schema-drift-detection-for-django/44382)

<div class="topic-metadata">

**Author:** [@ompatel29](https://forum.djangoproject.com/u/ompatel29)\
**Replies:** 2\
**Last updated:** [March 5, 2026, 6:53pm UTC](https://forum.djangoproject.com/t/deterministic-migration-replay-schema-drift-detection-for-django/44382 "2026-03-05T18:53:18Z")

</div>

Hello Django community, I’m exploring a potential improvement related to Django’s migration system and would appreciate feedback from maintainers and contributors. Problem In large production deployments, database sche…

---

## [UNNEST bulk\_create optimisation causes integer overflow for models referencing django\_admin\_log](https://forum.djangoproject.com/t/unnest-bulk-create-optimisation-causes-integer-overflow-for-models-referencing-django-admin-log/44272)

<div class="topic-metadata">

**Author:** [@frepettov0](https://forum.djangoproject.com/u/frepettov0)\
**Replies:** 5\
**Last updated:** [February 27, 2026, 3:53pm UTC](https://forum.djangoproject.com/t/unnest-bulk-create-optimisation-causes-integer-overflow-for-models-referencing-django-admin-log/44272 "2026-02-27T15:53:34Z")

</div>

The UNNEST bulk\_create optimisation introduced in Django 5.2 (ticket #35936) generates explicit PostgreSQL type casts derived from field.db\_type(). LogEntry does not declare an explicit id field. Its primary key type is…

---

## [Ticket #36915 - Request for Triage Review - select\_for\_update for implicit transactions](https://forum.djangoproject.com/t/ticket-36915-request-for-triage-review-select-for-update-for-implicit-transactions/44307)

<div class="topic-metadata">

**Author:** [@MANAS225](https://forum.djangoproject.com/u/MANAS225)\
**Replies:** 2\
**Last updated:** [February 27, 2026, 2:53pm UTC](https://forum.djangoproject.com/t/ticket-36915-request-for-triage-review-select-for-update-for-implicit-transactions/44307 "2026-02-27T14:53:21Z")

</div>

Hi everyone, I have been working on ticket #36915 (Add support for select\_for\_update for implicit transactions). The problem is that select\_for\_update() raises TransactionManagementError when used as a subquery insi…

---

## [Can the migration system be extended by third-party apps?](https://forum.djangoproject.com/t/can-the-migration-system-be-extended-by-third-party-apps/44009)

<div class="topic-metadata">

**Author:** [@CodenameTim](https://forum.djangoproject.com/u/CodenameTim)\
**Replies:** 3\
**Last updated:** [January 24, 2026, 2:40pm UTC](https://forum.djangoproject.com/t/can-the-migration-system-be-extended-by-third-party-apps/44009 "2026-01-24T14:40:53Z")

</div>

Hey folks, does the migration autodetector support being extended by third-party apps? I would like to have a custom model field which includes an extra operation after the AddField operation in the migration. The ultim…

---

## [Allow querying RelatedManagers on unsaved objects](https://forum.djangoproject.com/t/allow-querying-relatedmanagers-on-unsaved-objects/43962)

<div class="topic-metadata">

**Author:** [@rlaager](https://forum.djangoproject.com/u/rlaager)\
**Replies:** 2\
**Last updated:** [January 23, 2026, 6:26am UTC](https://forum.djangoproject.com/t/allow-querying-relatedmanagers-on-unsaved-objects/43962 "2026-01-23T06:26:29Z")

</div>

In commit 7ba6ebe9, to address #19580, accessing a RelatedManager on an unsaved model instance now raises a ValueError. The initial ticket comment said, “These are slight backwards compatibility changes, but to me it se…

[Next page](https://forum.djangoproject.com/c/internals/orm/9.md?page=1)
