Creating a column from another column in SQLAlchemy

I would like to have SQLAlchemy Columnthat will be computed from another column. For example, column month( {'Jan', ...}) from a column date(for example, 2014-09-12), so technically this is a one-time parsing 09before Sep.

Is it possible to write this to the class of the object itself? Or just run updates every time you update records?

PS: Months are taken, for example. I am interested in the general case when the space of a derived column is infinite.

+4
source share
2 answers

column_property . -, , .

:

class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)

    # actual database columns
    firstname = Column(String(50))   
    lastname = Column(String(50))

    # calculated from the other two
    fullname = column_property(firstname + " " + lastname)   

:

class Foo(Base):
    date = Column(DateTime)
    month_only = column_property(date.strptime("%b")) # 'Jan','Feb',etc.
+4
+2

All Articles