dmvianna
dmvianna

Reputation: 15718

add a virtual column to a reflected table

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

Answers (2)

Navaz Mannan
Navaz Mannan

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

van
van

Reputation: 76992

See Using column_property:

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

Related Questions