trigger_text = """
CREATE TRIGGER set_rank BEFORE INSERT ON authors
FOR EACH ROW BEGIN
    IF NEW.rank = 0 THEN
        SET NEW.rank = (SELECT IFNULL(MAX(a.rank),0) + 1
                        FROM authors AS a
                        WHERE a.id = NEW.pub_id);
    END IF;
END
"""
metadata = MetaData()
authors = Table('authors', metadata, Column('id', Integer, primary_key=True
    ), Column('rank', Integer), Column('pub_id', Integer))
engine = create_engine('mysql://root@127.0.0.1/test', echo=True)
DELIMITER / Создание триггера в SQLAlchemy