Reputation: 15718
I have a class
class TableA(Base):
__table__ = table_a
__mapper_args__ = {
'primary_key':[table_a.pk,],
}
and want to add a transform as a column that will be seen when querying (I only have read access, so no writing is necessary)
new_col = func.regexp_substr(table_a.original, r'[^-]*').label("new_col")
Is there a simple way of doing it?
Upvotes: 0
Views: 427
Reputation: 30
Using : https://docs.sqlalchemy.org/en/13/core/defaults.html#sqlalchemy.schema.FetchedValue
class TableA(Base):
........
new_col = Column(DB.String, FetchedValue())
........
Upvotes: 0
Reputation: 76992
class TableA(Base):
__table__ = table_a
__mapper_args__ = {
'primary_key':[table_a.pk,],
}
# new_col = column_property(func.regexp_substr(original, r'[^-]*')) # or
new_col = column_property(func.regexp_substr(table_a.c.original, r'[^-]*'))
Upvotes: 1