danni / django-postgres-composite-types

Postgres composite types support for Django
BSD 3-Clause "New" or "Revised" License
51 stars 13 forks source link

Not working as expression output_field in annotation/aggregation #27

Closed OJFord closed 2 years ago

OJFord commented 4 years ago
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.

danni commented 3 years ago

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

OJFord commented 3 years ago

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 commented 2 years ago

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 commented 2 years ago

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.