I hate to do this kind of thing, but I haven't gotten any feedback on this and I would love to hear some feedback/opinions. How do other people handle deferrable constraints?

On 2011-9-14 13:59, Wichert Akkerman wrote:
On 09/14/2011 12:25 PM, Wichert Akkerman wrote:
Constraints marked as deferrable result in a syntax error when using
SQLite. Is this deliberate, or a bug in the sqlite dialect?

As a workaround I figured I could use events to only add deferrable
constraint variants on PostgreSQL and use the non-deferrable version on
other database. That resulted in this code:

_generic_constraints = [UniqueConstraint('article_id', 'colour, 'size')]
_ deferrable_variant_constraints = [
UniqueConstraint('article_id', 'uuid', deferrable=True,
initially='DEFERRED'),
]

class Article(BaseObject):
__table_args = (_generic_constraints[0], _deferrable_constraints[0], {})

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 == '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 not deferrable_supported(ddl, target, bind, **kw)

for constraint in _generic_variant_constraints:
listen(Article.__table__, 'after_create',
AddConstraint(constraint)
.execute_if(callable_=deferrable_not_supported))

for constraint in _deferrable_variant_constraints:
listen(Article.__table__, 'after_create',
AddConstraint(constraint)
.execute_if(callable_=deferrable_supported))



But this fails as well since SQLite does not support ALTER TABLE .. ADD
CONSTRAINT. Is there another way to create deferrable constraints only
on databases that support it?

Wichert.





--
Wichert Akkerman <wich...@wiggy.net>   It is simple to make things.
http://www.wiggy.net/                  It is hard to make things simple.

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to