Add .cast shortcut method

Issue #3459 resolved
Sebastian Bank
created an issue

I often find myself expecting columns/expressions/orm attributes to have a .cast(type_) shortcut method (just as they provide .label, .desc, .op, etc.), such that one could write SELECT CAST(eggs AS text) FROM SPAM as

from sqlalchemy import select, Text

eggs_as_text = select([spam.c.eggs.cast(Text)])

(cf. postgres SELECT eggs::text FROM spam) instead of

from sqlalchemy import select, cast, Text

eggs_as_text = select([cast(spam.c.eggs, Text)])

With the recent addition of a cast method on postgresql.JSONElement, this might be a good occasion to add it (API consistency).

In a quick test, this wasn't more than two lines in sqlalchemy.sql.elements.ColumnElement, but maybe I forgot some edge cases.

Comments (5)

  1. Log in to comment