New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array of enums does not allow assigning #3467
Comments
Wichert Akkerman (@wichert) wrote: I tried to use an array literal as workaround: pizza = Pizza(toppings=array(['feta', 'spinach'], type_=topping)) this gave in the same error. |
Wichert Akkerman (@wichert) wrote: I've attached a minimal script to reproduce this. |
Changes by Wichert Akkerman (@wichert):
|
Michael Bayer (@zzzeek) wrote: no feature in PG, I already did all the work to identify this here: http://stackoverflow.com/a/9547795/34549, three years ago. Please email the psycopg2 list with our use case, as I think they should support it. Below, we illustrate ENUM works, ARRAY of VARCHAR works, ARRAY of ENUM does not. PG's support for UUID handles the ARRAY of UUID type, why can't they do ARRAY of ENUM?
|
Michael Bayer (@zzzeek) wrote: note that I'd rather not get into creating custom psycopg2 type casters within SQLAlchemy, because one day psycopg2 will implement the feature and then probably break against what we're doing (you never know when psycopg2 will add a new type handler either as always-on, e.g. JSON/JSONB, or optional, e.g. HSTORE, UUID). We also could do this with a CAST expression on the typing side. |
Michael Bayer (@zzzeek) wrote: though, we're sending in strings so psycopg2 wouldn't have any way of knowing we're even dealing with an ENUM. OK. Hmmmm..., we'd need a psycopg2 enum("mystring") type object from them, at the very least. |
Michael Bayer (@zzzeek) wrote: OK so what it wants is:
that's not too hard. Your test can run at the moment as:
|
Michael Bayer (@zzzeek) wrote: OK you can use this type:
and I can patch a feature:
|
Michael Bayer (@zzzeek) wrote: heh, well that's not enough. because here is what you get back:
|
Michael Bayer (@zzzeek) wrote: OK this is really the kind of thing where if I build this into the dialect, psycopg2 is going to suddenly work one day, so I'd rather just add this workaround to the docs for now:
let me know that this type object does everything you need, at least. |
Changes by Michael Bayer (@zzzeek):
|
Changes by Michael Bayer (@zzzeek):
|
Jacob Heller wrote: Your code is great (I even answered an SO question with it), except it raises an error if the retrieved value in the database is null. To fix that, I just added
to the top of the
method. |
Michael Bayer (@zzzeek) wrote: @jacobgh222 OK, well if the column is NULL we typically expect NULL to be returned. |
Michael Bayer (@zzzeek) wrote:
→ ba1e959 |
Michael Bayer (@zzzeek) wrote:
(cherry picked from commit ba1e959) Conflicts: → 7a3a5e9 |
Changes by Michael Bayer (@zzzeek):
|
Michael Bayer (@zzzeek) wrote: sorry, that's all i want to get into on this one for now, if I build it in it's suddenly going to just break one day as each DBAPI supports it. If people check with psycopg2 and can show that they have no plans to ever support this, we can reopen as a feature here. |
jrhite (@jrhite) wrote: Thanks. This works great assuming somewhere you map the enum type directly as a column. I only have my enum type mapped as an ArrayOfEnum. create_all() and drop_all() do not recognize the enum so it neither creates nor drops the type.
will fail if I actually try to create and insert a Party object. However simply doing:
works. Somewhere in at least one mapping, the actual enum type needs to be mapped directly and not via an ArrayOfEnum I've been digging around and can think of a couple of ugly solutions, but was wondering if there is a quick and elegant solution to this little problem. Ideas? Thanks much! |
Michael Bayer (@zzzeek) wrote: fixed in 1.1 http://docs.sqlalchemy.org/en/latest/changelog/migration_11.html#change-2729 |
jrhite (@jrhite) wrote: Thanks Mike! I pip installed SQLAlchemy==1.1.0b1.dev0 and looking good. I guess it's still handy to keep the ArrayOfEnum helper class around to get around the |
Michael Bayer (@zzzeek) wrote: this just came up for JSONB also. However psycopg2 seems to handle the "result" side without modification for that one (leaning towards the requires_bind_cast feature again). |
Michael Bayer (@zzzeek) wrote:
Change-Id: I9836b842be01ef24138071fa022d80f5f77be14f → 2b4d028 |
Michael Bayer (@zzzeek) wrote:
Change-Id: I9836b842be01ef24138071fa022d80f5f77be14f → c113b0e |
If the array is empty this raises a LookupError for me (due to returning ['']) - can modify to |
hi @vyh - this issue is three years old so I don't really know what your code excerpt refers to or how you are using it. can you please create a new issue with a complete example and full context, thanks |
Hello traveller from the future. Know that the docs now say
|
Migrated issue, originally created by Wichert Akkerman (@wichert)
There is an overlap with #2940 here.
I have an array of enums:
With that minimal model I try to creating a new pizza instance with some toppings:
which results in this error:
This is using SQLAlchemy 1.0.4 and psycopg2 2.5.4.
Attachments: x.py
The text was updated successfully, but these errors were encountered: