I have some custom DDL statements that I want to run after creating the table:
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(); """)
And I tied them like this:
event.listen(Session.__table__, 'after_create', update_function) event.listen(Session.__table__, 'after_create', update_trigger)
When I do create_all , I get the expected 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();
But when I update using Alembic, the statements do not appear:
-- 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');
Is there a way to get alembic to fire 'after_create' events?
David c
source share