Thanks for the help and suggestions. I understand that Constraint
objects need to be bound to a Table to drop or create them. I thought
there might be a general, straightforward way to find a Constraint by
Table and name. I did find a such a way for Constraints defined on the
Table. The approach I'm using for Constraints defined on Columns within
the Table works but seems kludgey. I had thought that since
CreateTable() automatically handles both cases, there might be a better way.
Rethinking it one more time, a Constraint defined within a Column is
expected to render a column constraint within a CREATE TABLE statement.
However, SQL provides no way to add a CHECK column constraint to a
column on an existing table. Therefore, I need to drop a column
constraint and then create a table constraint with the same name and
expression. So, as you said, defining the Constraint on the Table in the
first place would be more consistent.
I had looked at Alembic but it doesn't seem to quite match my use case.
Operations.drop_constraint() does what I need, but
Operations.create_check_constraint() does not seem to be able to use an
existing CheckConstraint instance. It also doesn't seem to have support
for Postgres's NOT VALID modifier. Maybe I'm missing something or maybe
it would be easy to extend Alembic to add these features?
--
Jonathan Rogers