django-postgres-composite-types icon indicating copy to clipboard operation
django-postgres-composite-types copied to clipboard

Not working as expression output_field in annotation/aggregation

Open OJFord opened this issue 4 years ago • 2 comments

Model.objects.annotate(composite=Func(1, 2, 3, function="construct_composite", output_field=CompositeField()))

results in

model.composite == '(1,2,3)'

i.e. a str rather than the expected (hoped for) CompositeType.

OJFord avatar Nov 11 '20 18:11 OJFord

Keen for someone to have a go at this. A PR with a failing test case would also be a great help.

danni avatar Nov 20 '20 22:11 danni

I do intend to try to get a test case for it - because weirdly I've actually seen it both ways now.

In our tests, and hence I initially thought it not working, it came through as a string as described. Then we saw an issue with it actually running where it was the composite type (the issue being that we tried to change it into one ourselves assuming a string, and it wasn't a string, so some string function didn't exist).

It's not immediately apparent whether the test is doing something differently and both behaviours could be seen in prod too (according to how the QuerySet is constructed, or something) or if it's some how inherent in test vs. real (quirk of its interaction with pytest-django, say). Not got to the bottom of it yet.

OJFord avatar Nov 21 '20 01:11 OJFord

Also seems to occur in a regularly declared field (i.e. on the model, not an annotation) when the model is managed = False (e.g. it's a view).

OJFord avatar Nov 14 '22 19:11 OJFord

Ah - nevermind, I forgot to register the type. I suspect this was also the case in my original report, and it worked outside of tests (per comment above) because the type was created outside of Django migrations.

OJFord avatar Nov 14 '22 20:11 OJFord