Luca
Luca

Reputation: 125

SQLAlchemy ORM: create 2 attributes for the same column

I'm working on the refactoring a legacy Alchemy ORM project, and for retro-compatibility reasons I need to keep some old attribute names and re-map them with a new name.

Alchemy ORM provide column_property(attribute) function that helps to create a read-only new attribute that reference an other attribute. The other known way to solve this problem is using hybrid_property:

@hybrid_property
def price(self):
    return self.old_price

@price.setter
def price(self, value):
    self.old_price = value

Is there any more concise way to alias getter and setter of column.

Upvotes: 1

Views: 308

Answers (1)

Ilja Everilä
Ilja Everilä

Reputation: 52997

For simple mirroring SQLAlchemy provides synonym():

class Foo(Base):
    old_price = Column(...)
    price = synonym("old_price")

Upvotes: 1

Related Questions