#35977: Serializing ManyToMany fields produces inconsistent order (on Postgres)
-------------------------------------+-------------------------------------
Reporter: Alexander Todorov | Type: New
| feature
Status: new | Component: Core
| (Serialization)
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
**Context:**
I am migrating data from one DB to another and using serialization to dump
the contents into JSON and then compare them with diff. On occasion it
happens that models which have a ManyToMany field result in a data dump
where the collection of PKs isn't sorted in exactly the same order when
comparing the 2 data dumps. This appears to happen quite often on
Postgres and I believe it is coming from Postgres itself - i.e. when an
explicit order isn't specified it sometimes returns the results in the
best way it sees fit.
**Proposal:**
Add a call to `.order_by("pk")` when handling m2m field serialization to
make the results predictable.
**Additional information**
I have a commit in my own fork,
https://github.com/atodorov/django/commit/1ae2f28ba42f28399f58d3edda98a35088225deb,
which works great for me.
LMK if you want me to open a pull request.
--
Ticket URL: <
https://code.djangoproject.com/ticket/35977>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.