Add .cast shortcut method
Issue #3459
resolved
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)
-
repo owner -
repo owner - changed milestone to 1.0.xx
-
assigned issue to
-
reporter -
repo owner - changed milestone to 1.0.7
-
repo owner - changed status to resolved
- changelog for
#3459, fixes#3459 - test for .cast() method has no good place now except for test_cast in test_compiler.py
→ <<cset c7312cc508ea>>
- Log in to comment
sure send a PR.