﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34697	Migration serializer for sets results in non-deterministic order.	Yury V. Zaytsev	nobody	"We are using quite a lot of complex `index_together` / `unique_together` constraints on our models, and the output in the generated migrations is flip-flopping all the time like follows, causing spurious diffs in our checkouts:

{{{
migrations.AlterUniqueTogether(
+    unique_together={(""tenant"", ""dealer""), (""tenant"", ""order"")},
-    unique_together={(""tenant"", ""order""), (""tenant"", ""dealer"")},
}}}

This is happening because these constraints are normalized to sets internally in the ModelState, which kind of makes sense, but unfortunately set iteration order (unlike dicts!) is unstable in Python 3 due to hash randomization. However, migrations serializer doesn't have any special facilities for ensuring stable output for sets and this is what causes annoying diffs for us all the time.

I suggest to add a trivial serializer specifically for unordered sequences which ensures stable output no matter the iteration order. Stability can be achieved by sorting elements in the set by their string representation. This only affects the writer output, and doesn't interfere with the rest of Django in any way, so this change only improves developer experience, but has no effect on the performance and/or reliability.

I hope that even though it's apparently not a major problem for most users you would still accept the fix to ensure stable migration writer output for the rest of us."	Cleanup/optimization	closed	Migrations	4.2	Normal	fixed		Simon Charette	Ready for checkin	1	0	0	0	0	0
