0

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?

2 Answers 2

1

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'[^-]*'))
0

Using : https://docs.sqlalchemy.org/en/13/core/defaults.html#sqlalchemy.schema.FetchedValue

class TableA(Base):
    ........
    new_col = Column(DB.String, FetchedValue())
    ........

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.