Silly example, but think this is what you're looking for, should get you going:
from sqlalchemy import event
from sqlalchemy.engine import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import create_session
from sqlalchemy.schema import Column, DDL
from sqlalchemy.types import Integer
Base = declarative_base()
engine = create_engine('sqlite:////tmp/test.db', echo=True)
class TableXYZ(Base):
__tablename__ = 'tablexyz'
id = Column(Integer, primary_key=True)
#event.listen(
# Base.metadata, 'after_create',
# DDL("""
# alter table TableXYZ add column name text
# """)
event.listen(
TableXYZ.__table__, 'after_create',
DDL("""
alter table TableXYZ add column name text
""")
)
Base.metadata.create_all(engine)
Running the above results in - note "name text" for the added column:
sqlite> .schema tablexyz
CREATE TABLE tablexyz (
id INTEGER NOT NULL, name text,
PRIMARY KEY (id)
);
I have my code in declarative and use the event.listen to add triggers and other stored procedures. Seems to work well.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…