python - How can I use a CAST in an ExcludeConstraint in SQLAlchemy? -


in postgresql can create table exclusion constraint involving cast (the cast necessary because uuid type doesn't have default operator class gist):

create extension btree_gist; create table example (   id         uuid,   some_range int4range,   exclude using gist (cast("id" text) =, some_range &&) ); 

i can't figure out how accomplish same thing in sqlalchemy. tried this:

from sqlalchemy import * sqlalchemy.dialects.postgresql import (     uuid, int4range, excludeconstraint, text )  class example(base):     __tablename__ = 'example'     id = column(uuid)     some_range = column(int4range)     __table_args__ = (         excludeconstraint(             (cast('id', text), '='), ('some_range', '&&')         ),     ) 

but error sqlalchemy.exc.argumenterror: can't add unnamed column column collection.

how can sqlalchemy use cast in excludeconstraint? alternatively, how can use raw sql define exclude constraint on table?

from https://bitbucket.org/zzzeek/sqlalchemy/issue/3454, workaround (for versions prior 1.0.6) is

from sqlalchemy.sql.elements import quoted_name      excludeconstraint(         (column(quoted_name('cast("id" text)', quote=false)), '='), ('some_range', '&&')     ), 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -