[Sqlalchemy-tickets] Issue #4160: Add postgresl.regclass type for casting tablenames to oids (zzzee
Brought to you by:
zzzeek
From: Sebastian B. <iss...@bi...> - 2018-01-11 16:05:33
|
New issue 4160: Add postgresl.regclass type for casting tablenames to oids https://bitbucket.org/zzzeek/sqlalchemy/issues/4160/add-postgreslregclass-type-for-casting Sebastian Bank: Postgresql provides the `regclass` type to [simplify system catalog queries](https://www.postgresql.org/docs/10/static/datatype-oid.html) (reflection): > The regclass input converter handles the table lookup according to the schema path setting, and so it does the “right thing” automatically. Similarly, casting a table's OID to regclass is handy for symbolic display of a numeric OID. I think it would be nice (and not too hard) to provide this in `sqlalchemy`. Maybe like this: ```python In [1]: import sqlalchemy as sa ...: ...: engine = sa.create_engine('postgresql://postgres@') ...: ...: class regclass(sa.types.UserDefinedType): ...: def get_col_spec(self): ...: return 'regclass' ...: ...: pga = sa.table('pg_attribute', ...: *map(sa.column, ['attrelid', 'attname', 'attnotnull'])) ...: ...: query = pga.select()\ ...: .where(pga.c.attrelid == sa.cast('pg_class', regclass))\ ...: .where(~pga.c.attnotnull) ...: ...: engine.execute(query).fetchall() Out[1]: [(1259, 'relacl', False), (1259, 'reloptions', False)] In [2]: engine.scalar(sa.select([sa.cast(1259, regclass)])) Out[2]: 'pg_class' ``` |