# ORM

**URL:** https://forum.djangoproject.com/c/internals/orm/9.md?page=2

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

**Page:** 3

---

## [Migration optimizer does not optimize choices](https://forum.djangoproject.com/t/migration-optimizer-does-not-optimize-choices/4645)

<div class="topic-metadata">

**Author:** [@fmarani](https://forum.djangoproject.com/u/fmarani)\
**Replies:** 5\
**Last updated:** [June 12, 2025, 10:39pm UTC](https://forum.djangoproject.com/t/migration-optimizer-does-not-optimize-choices/4645 "2025-06-12T22:39:16Z")

</div>

We have a lot of tables in our db which have a lot of category-type fields, and those charfields have a fixed set of choices that have been growing in the history of the project. First migration: migrations.Cre…

---

## [Lookup using annotation from a subquery](https://forum.djangoproject.com/t/lookup-using-annotation-from-a-subquery/40911)

<div class="topic-metadata">

**Author:** [@PierreMDL](https://forum.djangoproject.com/u/PierreMDL)\
**Replies:** 4\
**Last updated:** [May 14, 2025, 11:21am UTC](https://forum.djangoproject.com/t/lookup-using-annotation-from-a-subquery/40911 "2025-05-14T11:21:44Z")

</div>

Hello everybody, My Product model can have multiple children Tagging models, the last of which is of special importance: class Product(Model): name = CharField(...) class Tagging(Model): class Meta: or…

---

## [KeyError when migrating backward with a squashed migration includes RemoveField operation](https://forum.djangoproject.com/t/keyerror-when-migrating-backward-with-a-squashed-migration-includes-removefield-operation/40704)

<div class="topic-metadata">

**Author:** [@fabien-michel](https://forum.djangoproject.com/u/fabien-michel)\
**Replies:** 6\
**Last updated:** [May 5, 2025, 2:28pm UTC](https://forum.djangoproject.com/t/keyerror-when-migrating-backward-with-a-squashed-migration-includes-removefield-operation/40704 "2025-05-05T14:28:48Z")

</div>

Hi, I thinks there is a bug in django in that particular conditions. When an app has a squashed migration which include a RemoveField operation and you try to migrate backward on a migration elsewhere (in the app or in…

---

## [FutureDateTime and PastDateTime validators](https://forum.djangoproject.com/t/futuredatetime-and-pastdatetime-validators/40553)

<div class="topic-metadata">

**Author:** [@deronnax](https://forum.djangoproject.com/u/deronnax)\
**Replies:** 11\
**Last updated:** [April 27, 2025, 11:44am UTC](https://forum.djangoproject.com/t/futuredatetime-and-pastdatetime-validators/40553 "2025-04-27T11:44:12Z")

</div>

Hello. Long time django user here. Over the year, I found myself writing again and again, from projects to projects, a PastDatetimeValidator (e.g for birth date, or generally thing that happened) and FuturDateTimeValid…

---

## [Allow Meta class to have extra Database specific options](https://forum.djangoproject.com/t/allow-meta-class-to-have-extra-database-specific-options/40650)

<div class="topic-metadata">

**Author:** [@surister](https://forum.djangoproject.com/u/surister)\
**Replies:** 2\
**Last updated:** [April 26, 2025, 11:09am UTC](https://forum.djangoproject.com/t/allow-meta-class-to-have-extra-database-specific-options/40650 "2025-04-26T11:09:10Z")

</div>

Hi, I’m developing a driver for CrateDB. GitHub - crate/cratedb-django: CrateDB backend for Django CrateDB’s SQL dialect has extra functionalities, I’ll give you two examples: you can create a table clustered in X sha…

---

## [Idea: {make,squash}migrations --no-deps](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986)

<div class="topic-metadata">

**Author:** [@shaib](https://forum.djangoproject.com/u/shaib)\
**Replies:** 11\
**Last updated:** [April 22, 2025, 4:00pm UTC](https://forum.djangoproject.com/t/idea-make-squash-migrations-no-deps/23986 "2025-04-22T16:00:20Z")

</div>

Hi, I’ve been thinking about the problem of large projects with many models and migrations, and circular cross-app dependencies. In such projects, squashmigrations typically fails, and in many cases, one cannot even “de…

---

## [PR #19004 Advice from the ORM team](https://forum.djangoproject.com/t/pr-19004-advice-from-the-orm-team/40474)

<div class="topic-metadata">

**Author:** [@gmaOCR](https://forum.djangoproject.com/u/gmaOCR)\
**Replies:** 1\
**Last updated:** [April 18, 2025, 10:16pm UTC](https://forum.djangoproject.com/t/pr-19004-advice-from-the-orm-team/40474 "2025-04-18T22:16:39Z")

</div>

On Nessita’s advice, I’m here to gather information and opinions about the breakdown required for this PR (and the related ticket). To handle the division operation depending on variable types, I had to propagate output…

---

## [Possible Bug: .values("pk") and .values("id") generate different SQL since 5.2](https://forum.djangoproject.com/t/possible-bug-values-pk-and-values-id-generate-different-sql-since-5-2/40469)

<div class="topic-metadata">

**Author:** [@maqnius](https://forum.djangoproject.com/u/maqnius)\
**Replies:** 2\
**Last updated:** [April 17, 2025, 1:56pm UTC](https://forum.djangoproject.com/t/possible-bug-values-pk-and-values-id-generate-different-sql-since-5-2/40469 "2025-04-17T13:56:45Z")

</div>

I encountered a bug in combination with the django\_cte package but I’m not entirely sure if it’s only a django\_cte bug and if the behaviour is really intended in Django 5.2. Since Django 5.2, querying the primary keys u…

---

## [Database Features by DB Provider for Unit Tests](https://forum.djangoproject.com/t/database-features-by-db-provider-for-unit-tests/40412)

<div class="topic-metadata">

**Author:** [@WayneLambert](https://forum.djangoproject.com/u/WayneLambert)\
**Replies:** 4\
**Last updated:** [April 16, 2025, 6:36pm UTC](https://forum.djangoproject.com/t/database-features-by-db-provider-for-unit-tests/40412 "2025-04-16T18:36:08Z")

</div>

Hi, I have been looking into the testing within Django’s test suite with regards to tests that are skipped depending upon the features supported by a given database provider. Django natively supports Postgres, MariaDB,…

---

## [QuerySet ".update(...)" silently turns into SELECT+UPDATE? (MySQL)](https://forum.djangoproject.com/t/queryset-update-silently-turns-into-select-update-mysql/39095)

<div class="topic-metadata">

**Author:** [@bbkfhq](https://forum.djangoproject.com/u/bbkfhq)\
**Replies:** 6\
**Last updated:** [April 7, 2025, 3:27am UTC](https://forum.djangoproject.com/t/queryset-update-silently-turns-into-select-update-mysql/39095 "2025-04-07T03:27:46Z")

</div>

TL;DR: When a QuerySet is being filtered based on related objects, executing .update(...) generates SELECT-then-UPDATE instead of a single UPDATE? I have a query like this: Choice.objects.filter(pk=1) \\ .fi…

---

## [Selecting primary key in 5.2](https://forum.djangoproject.com/t/selecting-primary-key-in-5-2/40215)

<div class="topic-metadata">

**Author:** [@simwr872](https://forum.djangoproject.com/u/simwr872)\
**Replies:** 2\
**Last updated:** [April 5, 2025, 6:47pm UTC](https://forum.djangoproject.com/t/selecting-primary-key-in-5-2/40215 "2025-04-05T18:47:36Z")

</div>

Hello, unsure if this counts a regression or not so I thought I would check here. Normally you would not care about the raw SQL produced by the ORM. But after upgrading from 5.1.7 to 5.2.0 some queries using a third part…

---

## [My Group By Aggregations are Breaking in 5.2](https://forum.djangoproject.com/t/my-group-by-aggregations-are-breaking-in-5-2/40162)

<div class="topic-metadata">

**Author:** [@paltman](https://forum.djangoproject.com/u/paltman)\
**Replies:** 1\
**Last updated:** [April 3, 2025, 2:13am UTC](https://forum.djangoproject.com/t/my-group-by-aggregations-are-breaking-in-5-2/40162 "2025-04-03T02:13:50Z")

</div>

When I upgraded to 5.2 today, a number of my query sets broke tracebacks on building the query. I didn’t notice anything in the BIC of the release notes. Before I file a proper ticket, I wanted to make sure I wasn’t ju…

---

## [Django not recognizing library](https://forum.djangoproject.com/t/django-not-recognizing-library/40128)

<div class="topic-metadata">

**Author:** [@remoteconn-7891](https://forum.djangoproject.com/u/remoteconn-7891)\
**Replies:** 8\
**Last updated:** [April 2, 2025, 11:28am UTC](https://forum.djangoproject.com/t/django-not-recognizing-library/40128 "2025-04-02T11:28:51Z")

</div>

Hello, I’m using GeoDjango for one the apps in my Django project. Anyway, it’s not recognizing OSMGeoAdmin in my admin.py here from django.contrib.auth import get\_user\_model from django.contrib import admin from .models…

---

## [Reusable library and incompatible Django core changes](https://forum.djangoproject.com/t/reusable-library-and-incompatible-django-core-changes/40097)

<div class="topic-metadata">

**Author:** [@nijel](https://forum.djangoproject.com/u/nijel)\
**Replies:** 3\
**Last updated:** [April 1, 2025, 2:02pm UTC](https://forum.djangoproject.com/t/reusable-library-and-incompatible-django-core-changes/40097 "2025-04-01T14:02:06Z")

</div>

We are developing a library and want to use CheckConstraint in our code. We intend to support all Django supported versions with a single codebase. Unfortunately, the depreciation and removal schedule make it tricky. Dj…

---

## [first() Method Does Not Use prefetch\_related](https://forum.djangoproject.com/t/first-method-does-not-use-prefetch-related/39452)

<div class="topic-metadata">

**Author:** [@akhilgargjpr](https://forum.djangoproject.com/u/akhilgargjpr)\
**Replies:** 4\
**Last updated:** [March 15, 2025, 7:33pm UTC](https://forum.djangoproject.com/t/first-method-does-not-use-prefetch-related/39452 "2025-03-15T19:33:43Z")

</div>

I’ve discovered that when using Django’s first() method on a queryset that has prefetch\_related applied, the prefetched relationships are not utilized in the returned object. This leads to unexpected behavior and potenti…

---

## [Prefetching relations to arbitrary depth](https://forum.djangoproject.com/t/prefetching-relations-to-arbitrary-depth/39328)

<div class="topic-metadata">

**Author:** [@jacobtylerwalls](https://forum.djangoproject.com/u/jacobtylerwalls)\
**Replies:** 8\
**Last updated:** [March 14, 2025, 4:12am UTC](https://forum.djangoproject.com/t/prefetching-relations-to-arbitrary-depth/39328 "2025-03-14T04:12:59Z")

</div>

I’m wondering if we can make it simpler to prefetch relations recursively. I have trees modeled with a self-referring foreign key (“adjacency list” trees), like this model in the Django test suite I’ve edited to have a…

---

## [Automatically setting update\_fields when saving a model instance](https://forum.djangoproject.com/t/automatically-setting-update-fields-when-saving-a-model-instance/26798)

<div class="topic-metadata">

**Author:** [@nickaddison](https://forum.djangoproject.com/u/nickaddison)\
**Replies:** 7\
**Last updated:** [March 13, 2025, 10:14pm UTC](https://forum.djangoproject.com/t/automatically-setting-update-fields-when-saving-a-model-instance/26798 "2025-03-13T22:14:12Z")

</div>

After running into concurrency issues where my Django model instances were being overwritten by previous values due to .save() not having update\_fields specified, I began refactoring to specify update\_fields wherever pos…

---

## [ManyToManyField Error message with Incorrect "to" argument](https://forum.djangoproject.com/t/manytomanyfield-error-message-with-incorrect-to-argument/39394)

<div class="topic-metadata">

**Author:** [@JordanHyatt](https://forum.djangoproject.com/u/JordanHyatt)\
**Replies:** 8\
**Last updated:** [March 8, 2025, 11:02pm UTC](https://forum.djangoproject.com/t/manytomanyfield-error-message-with-incorrect-to-argument/39394 "2025-03-08T23:02:30Z")

</div>

When passing a string as the “to” argument in ManyToManyField, if you make a mistake or have a typo in the name of the related model you get the following AttributeError. AttributeError: 'str' object has no attribute '…

---

## [Proposal: Database Router hints](https://forum.djangoproject.com/t/proposal-database-router-hints/39156)

<div class="topic-metadata">

**Author:** [@limugob](https://forum.djangoproject.com/u/limugob)\
**Replies:** 3\
**Last updated:** [March 3, 2025, 5:31pm UTC](https://forum.djangoproject.com/t/proposal-database-router-hints/39156 "2025-03-03T17:31:44Z")

</div>

Hello, I would like to propose, to allow to add hints for the database router on all operations in migrations, e.g. migrations.CreateModel(..., hints={"just\_analytics\_db": True}), because this would sometimes simplify t…

---

## [New feature request: exists\_or\_create](https://forum.djangoproject.com/t/new-feature-request-exists-or-create/38944)

<div class="topic-metadata">

**Author:** [@JordanHyatt](https://forum.djangoproject.com/u/JordanHyatt)\
**Replies:** 3\
**Last updated:** [February 18, 2025, 10:55pm UTC](https://forum.djangoproject.com/t/new-feature-request-exists-or-create/38944 "2025-02-18T22:55:09Z")

</div>

Add a new Manager method called exists\_or\_create. This method would be a compliment to the get\_or\_create method with the key difference being it would simply check for existence or create, rather than loading the object…

---

## [Breaking a lance for "get\_or\_none" feature](https://forum.djangoproject.com/t/breaking-a-lance-for-get-or-none-feature/38730)

<div class="topic-metadata">

**Author:** [@GitRon](https://forum.djangoproject.com/u/GitRon)\
**Replies:** 10\
**Last updated:** [February 11, 2025, 10:21am UTC](https://forum.djangoproject.com/t/breaking-a-lance-for-get-or-none-feature/38730 "2025-02-11T10:21:00Z")

</div>

Hi folks! TLDR; We should add a helper similar to get\_or\_create to fetch one object and return None if none exist. User Story As a Django developer I want to efficiently fetch an object where I’m not 100% sure that thi…

---

## [Ticket #10244 - FileField can't be set to NULL in the database](https://forum.djangoproject.com/t/ticket-10244-filefield-cant-be-set-to-null-in-the-database/37137)

<div class="topic-metadata">

**Author:** [@adamzap](https://forum.djangoproject.com/u/adamzap)\
**Replies:** 7\
**Last updated:** [February 11, 2025, 3:53am UTC](https://forum.djangoproject.com/t/ticket-10244-filefield-cant-be-set-to-null-in-the-database/37137 "2025-02-11T03:53:09Z")

</div>

I’m seeking consensus and guidance on how to proceed with Ticket #10244. My personal leaning is to agree with the second option that Aymeric Augustin offered: Make FileField behave exactly like CharField with regard to n…

---

## [Ability to specify keys for JSONObject using arbitrary expressions](https://forum.djangoproject.com/t/ability-to-specify-keys-for-jsonobject-using-arbitrary-expressions/38559)

<div class="topic-metadata">

**Author:** [@john-parton](https://forum.djangoproject.com/u/john-parton)\
**Replies:** 3\
**Last updated:** [February 5, 2025, 10:52pm UTC](https://forum.djangoproject.com/t/ability-to-specify-keys-for-jsonobject-using-arbitrary-expressions/38559 "2025-02-05T22:52:10Z")

</div>

Currently there is no way to build a JSON object in the database using the JSONObject function where the keys are anything but static keys. This is not a limitation of any of the backends that support JSON objects. As …

---

## [django bug? sqlmigrate and unique\_together: ValueError: Found wrong number (0) of constraints](https://forum.djangoproject.com/t/django-bug-sqlmigrate-and-unique-together-valueerror-found-wrong-number-0-of-constraints/38516)

<div class="topic-metadata">

**Author:** [@deronnax](https://forum.djangoproject.com/u/deronnax)\
**Replies:** 0\
**Last updated:** [February 3, 2025, 4:30pm UTC](https://forum.djangoproject.com/t/django-bug-sqlmigrate-and-unique-together-valueerror-found-wrong-number-0-of-constraints/38516 "2025-02-03T16:30:48Z")

</div>

Hello. (first time poster here) I think I found a bug with sqlmigrate. In a model, I went from this unique constraint: class Meta: unique\_together = \["flight\_number", "airline", "scheduled\_departure"\] To…

---

## [create Abstract Model with some commum field which should be implement in child](https://forum.djangoproject.com/t/create-abstract-model-with-some-commum-field-which-should-be-implement-in-child/38281)

<div class="topic-metadata">

**Author:** [@batajoie](https://forum.djangoproject.com/u/batajoie)\
**Replies:** 0\
**Last updated:** [January 27, 2025, 4:03pm UTC](https://forum.djangoproject.com/t/create-abstract-model-with-some-commum-field-which-should-be-implement-in-child/38281 "2025-01-27T16:03:43Z")

</div>

Hello Hi everyone, Im trying to create abstract model with some commun field which should be define in child. The field could have different name depend on the child. But the contraint of this field should be the same …

---

## [Azure managed identity MySQL django backend.](https://forum.djangoproject.com/t/azure-managed-identity-mysql-django-backend/38093)

<div class="topic-metadata">

**Author:** [@stepanh](https://forum.djangoproject.com/u/stepanh)\
**Replies:** 0\
**Last updated:** [January 20, 2025, 5:20pm UTC](https://forum.djangoproject.com/t/azure-managed-identity-mysql-django-backend/38093 "2025-01-20T17:20:18Z")

</div>

I was recently trying to make django authenticate into Azure MySQL using azure managed identity, and developed a simple package/hack to accomplish that. The main difficulty was that Azure MySQL expects a security token i…

---

## [Persistant connections doesn't work properly after migrating to Django 4.2](https://forum.djangoproject.com/t/persistant-connections-doesnt-work-properly-after-migrating-to-django-4-2/25321)

<div class="topic-metadata">

**Author:** [@humanbeing-dev](https://forum.djangoproject.com/u/humanbeing-dev)\
**Replies:** 4\
**Last updated:** [January 9, 2025, 2:24pm UTC](https://forum.djangoproject.com/t/persistant-connections-doesnt-work-properly-after-migrating-to-django-4-2/25321 "2025-01-09T14:24:32Z")

</div>

Hi, after migrating from django 3.2 to 4.2 I’ve noticed persistant connections doesn’t work the same for the new version. I’m using CONN\_MAX\_AGE=20 setting For Django 3.2 I can see that oracle doesn’t create new sessi…

---

## [Add \`connection\_closed\` or \`pre\_connection\_closed\` and \`post\_connection\_closed\` signals](https://forum.djangoproject.com/t/add-connection-closed-or-pre-connection-closed-and-post-connection-closed-signals/37819)

<div class="topic-metadata">

**Author:** [@tomwojcik](https://forum.djangoproject.com/u/tomwojcik)\
**Replies:** 0\
**Last updated:** [January 9, 2025, 10:08am UTC](https://forum.djangoproject.com/t/add-connection-closed-or-pre-connection-closed-and-post-connection-closed-signals/37819 "2025-01-09T10:08:52Z")

</div>

Currently I’m facing an issue with stale DB connections. It’s probably related to my setup. Having connection\_closed signal would simplify my debugging process. Since 2009, there’s connection\_created signal, added in 6f…

---

## [Ideas for implementing Datetime infinity for postgres in Django ORM](https://forum.djangoproject.com/t/ideas-for-implementing-datetime-infinity-for-postgres-in-django-orm/37802)

<div class="topic-metadata">

**Author:** [@kavineverstage](https://forum.djangoproject.com/u/kavineverstage)\
**Replies:** 1\
**Last updated:** [January 9, 2025, 8:03am UTC](https://forum.djangoproject.com/t/ideas-for-implementing-datetime-infinity-for-postgres-in-django-orm/37802 "2025-01-09T08:03:48Z")

</div>

Django ORM does not support Infinity as a value for datetime fields, however postgres does. But the open ticket was closed with the reason being that there is no clear solution on how to implement this feature. It was su…

---

## [AutoField in CompositePrimaryKey](https://forum.djangoproject.com/t/autofield-in-compositeprimarykey/37334)

<div class="topic-metadata">

**Author:** [@csirmazbendeguz](https://forum.djangoproject.com/u/csirmazbendeguz)\
**Replies:** 0\
**Last updated:** [December 19, 2024, 4:30pm UTC](https://forum.djangoproject.com/t/autofield-in-compositeprimarykey/37334 "2024-12-19T16:30:27Z")

</div>

Since CompositePrimaryKey is already in core (#373), we need to reach a consensus on whether we want to add AutoField support for it or not: class User(Model): pk = CompositePrimaryKey("tenant\_id", "id") tenant\_…

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

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