我有几个要在创建表后运行的自定义 DDL 语句:
update_function = DDL("""
CREATE OR REPLACE FUNCTION update_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ language 'pgplsql';
""")
update_trigger = DDL("""
CREATE TRIGGER update %(table)s_timestamp BEFORE UPDATE
ON %(table)s FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
""")
我已经像这样附加它们:
event.listen(Session.__table__, 'after_create', update_function)
event.listen(Session.__table__, 'after_create', update_trigger)
当我这样做时create_all
,我得到了我期望的 SQL:
CREATE OR REPLACE FUNCTION update_timestamp()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = now();
RETURN NEW;
END;
$$ language 'pgplsql';
CREATE TRIGGER update session_timestamp BEFORE UPDATE
ON session FOR EACH ROW EXECUTE PROCEDURE update_timestamp();
但是当我使用 Alembic 升级时,语句不会出现:
-- Running upgrade c0d470e5c81 -> 6692fad7378
CREATE TABLE session (
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT 'CURRENT_TIMESTAMP',
updated_at TIMESTAMP WITHOUT TIME ZONE DEFAULT 'CURRENT_TIMESTAMP',
id VARCHAR(32) NOT NULL,
owner_id INTEGER,
database_id VARCHAR(32),
content TEXT,
PRIMARY KEY (id),
FOREIGN KEY(database_id) REFERENCES database (id),
FOREIGN KEY(owner_id) REFERENCES users (id)
);
INSERT INTO alembic_version (version_num) VALUES ('6692fad7378');
有没有办法让 alembic 触发“after_create”事件?