SQLAlchemy’s documentation provides pretty clear directions on how to execute DDL statements before the creation of a specific table using code like
DDL(statement).execute_at('before-create', table)
But what if I want to be sure that the statement is executed before multiple tables are created? In the simplest case, let’s say I want to make sure that the statement is executed before any tables are created. Can I have them execute at before-create for the model Base class’s metadata, like so?
DDL(statement).execute_at('before-create', Base.metadata)
(I’m concerned that the ‘before-create’ here might mean that the statement is being executed every single time that a table is being created.)
simple answer, “yes”, just the way you’re indicating it there. The event is good for Table and MetaData.
However, if you’re on at least 0.7, you should use the new event format. See http://docs.sqlalchemy.org/en/latest/core/events.html#sqlalchemy.events.DDLEvents