[Sqlalchemy-tickets] Issue #3339: AddConstraint example fails with binding error (zzzeek/sqlalchemy
Brought to you by:
zzzeek
|
From: Wichert A. <iss...@bi...> - 2015-03-23 15:07:47
|
New issue 3339: AddConstraint example fails with binding error https://bitbucket.org/zzzeek/sqlalchemy/issue/3339/addconstraint-example-fails-with-binding Wichert Akkerman: In order to support mark a constraint as deferrable with dialects that support it I use this code: ```python def deferrable_supported(ddl, target, bind, **kw): """Check if deferrable constraints are supported. This function can be used as a callable for :ref:`execute_if <sqlalchemy:sqlalchemy.schema.DDLElement.execute_if>` to only run DDL statements on databases that support deferrable constraints. """ return bind.dialect.name == 'postgresql' def deferrable_not_supported(ddl, target, bind, **kw): """Check if deferrable constraints are not supported. This function can be used as a callable for :ref:`execute_if <sqlalchemy:sqlalchemy.schema.DDLElement.execute_if>` to only run DDL statements on databases that do not support deferrable constraints. """ return bind.dialect.name not in ('postgresql', 'sqlite') for constraint in _generic_sku_constraints: listen(SKU.__table__, 'after_create', schema.AddConstraint(constraint) .execute_if(callable_=deferrable_not_supported)) for constraint in _deferrable_sku_constraints: listen(SKU.__table__, 'after_create', schema.AddConstraint(constraint) .execute_if(callable_=deferrable_supported)) ``` This code is modeled on the example in the [DDL chapter](http://docs.sqlalchemy.org/en/rel_0_9/core/ddl.html). This code fails with SQLAlchemy 0.9 with a `InvalidRequestError: This constraint is not bound to a table. Did you mean to call table.append_constraint(constraint) ?` error. |