Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
Arrays of ranges don't adapt well #313
Django 1.8 has introduced new fields for both ranges and arrays. They both work really well in isolation, but the combination (e.g.
The basic reason for this is that while the following statement works:
This one does not:
It must become
I believe we should be able to fix this at the Django end by overriding the registered adapter for arrays of range types, however if the psycopg2 auto registered adapter doesn't work, this seemed like a bug here.
What are your thoughts? Is this something we should fix, you should fix or both?
More details can be found at https://code.djangoproject.com/ticket/24726, including some pscopg2 example code which generates the error.
What you may do, because you know more precisely the type of range you are adapting, could be to subclass
and its adapter:
these should adapt correctly:
We didn't use this method because generally a Python user will have numbers in python without a stricter specification of their type. I'll think about introducing something similar for psycopg if it's practical enough. However, because Django does have type specification, it should be possible for it to use this system.
Further discussion is happening in the django bug. The shortcoming reported is common to many uses of arrays of special types; psycopg is not in a good position to solve that, because adaptation is based on types; for python a list is a list is a list, whatever it contains, whereas for postgres lists are of a specific type.
Because the client usually knows what type the list contains, the workaround suggested is to add a cast to the placeholder, e.g.