Search code examples
pythonpropertiessqlalchemyautomatic-properties

Auto updating properties in sqlalchemy


I've got a sqlalchemy model that is set up like this:

class Entry(Base):
    __tablename__ = 'entries'
    __table__ = Table('entries', Base.metadata,
            Column('id', Integer, primary_key=True, unique=True),
            Column('user_id', Integer, ForeignKey('users.id', onupdate="CASCADE", ondelete="RESTRICT")),
            Column('title', String(128)),
            Column('slug', String(128), index=True),
            Column('url', String(256), index=True),
            Column('entry', Text),
            Column('cached_entry', Text),
            Column('created', DateTime, server_default=text('current_timestamp')),
            Column('modified', DateTime, server_onupdate=text('current_timestamp')),
            Column('pubdate', DateTime),
            )

What I would like is that when I update entry that cached_entry gets re-generated, cached_entry is the markdown parsed version of entry. Basically I am caching the output of the markdown parsing so that I don't have to do it on each showing of the entry. I've ttried using @hybrid_method however that didn't seem to work as that is not stored in the database at all. I've got it working on Google AppEngine, but I can't seem to figure out how to do the same thing using SQLAlchemy.

I really would prefer not to have to add a function to the class that is used instead of the names in the model because it is harder to enforce it from an application standpoint, I don't want to accidentally miss something.


Solution

  • @hybrid_descriptor certainly does it using the form described at http://www.sqlalchemy.org/docs/orm/mapper_config.html#using-descriptors . You assign to the database-mapped attribute, which you can map under a different name - since you're using the __table__, you can use a form like:

    class Entry(Base):
        __table__ = ...
    
        _entry = __table__.c.entry
    
        @hybrid_property
        def entry(self):
            return self._entry
    
        @entry.setter
        def entry(self, value):
            self._entry = value
            self.cached_entry = markdown(value)
    

    Another is to use the before_insert and before_update events to populate the column at flush time - this is a simple approach but has the disadvantage that you have to wait for a flush() for it to happen.

    I think the quickest way for an "on-set" is to use @validates:

    from sqlalchemy.orm import validates
    
    class Entry(Base):
        __table__ = ...
    
        @validates('entry')
        def _set_entry(self, key, value):
            self.cached_entry = markdown(value)
            return value