Why aren't my Django Postgres `ArrayAgg` members sorting?

I'm exploring the use of ArrayAgg and I don't understand why 'histidine-[13C6,15N3]' doesn't occur before 'isoleucine-[13C6,15N1]' in this example:

In [25]: for i in Infusate.objects.annotate(tns=ArrayAgg("tracer_links__tracer__name", order_by="tracer_links__tracer__name")).order_by("tns").distinct():
    ...:     print(i.tns)
    ...: 
['inosine-[15N4]']
['isoleucine-[13C6,15N1]', 'leucine-[13C6,15N1]', 'valine-[13C5,15N1]']
['isoleucine-[13C6,15N1]', 'lysine-[13C6,15N2]', 'phenylalanine-[13C9,15N1]', 'threonine-[13C4,15N1]', 'tryptophan-[13C11]', 'histidine-[13C6,15N3]']

The records/rows are sorted based on the first value, which is great, but that first value isn't sorted WRT the other values in the list.

For that matter, why doesn't the order of the members of the list change at all when I negate the order_by in the ArrayAgg? According to what I read, this should sort the list items. Am I blind? What am I doing wrong?

In [25]: for i in Infusate.objects.annotate(tns=ArrayAgg("tracer_links__tracer__name", order_by="-tracer_links__tracer__name")).order_by("tns").distinct():
    ...:     print(i.tns)
    ...: 
['inosine-[15N4]']
['isoleucine-[13C6,15N1]', 'leucine-[13C6,15N1]', 'valine-[13C5,15N1]']
['isoleucine-[13C6,15N1]', 'lysine-[13C6,15N2]', 'phenylalanine-[13C9,15N1]', 'threonine-[13C4,15N1]', 'tryptophan-[13C11]', 'histidine-[13C6,15N3]']

D'oh! I'm on Django 4.2 and I was looking at the 5.2 documentation! It's ordering in 4.2. order_by is 5.2:

In [28]: for i in Infusate.objects.annotate(tns=ArrayAgg("tracer_links__tracer__name", ordering="tracer_links__tracer__name")).order_by("tns").distinct():
    ...:     print(i.tns)
    ...: 
['histidine-[13C6,15N3]', 'isoleucine-[13C6,15N1]', 'lysine-[13C6,15N2]', 'phenylalanine-[13C9,15N1]', 'threonine-[13C4,15N1]', 'tryptophan-[13C11]']
['inosine-[15N4]']
['isoleucine-[13C6,15N1]', 'leucine-[13C6,15N1]', 'valine-[13C5,15N1]']
Вернуться на верх