sqlalchemy is ignoring the CheckConstraint name parameter while creating a check constraint

317 views
Skip to first unread message

Mariano Mara

unread,
Oct 21, 2011, 7:10:01 PM10/21/11
to sqlalchemy
Hi all,
the subject pretty much explains it all. Here's a complete example of the
issue. Any tips will be appreciate.

Regards,
Mariano

Python 2.7.2 (default, Oct 14 2011, 23:34:02)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlalchemy
>>> sqlalchemy.__version__
'0.7.3'
>>> from sqlalchemy import Table, Column, Integer, create_engine, MetaData, CheckConstraint
>>> from sqlalchemy.orm import sessionmaker
>>> session = sessionmaker()
>>> Session = session()
>>> engine = create_engine('postgresql://usr:passws@localhost/base', echo=True)
>>> Session.configure(bind=engine)
>>> md = MetaData()
>>> md.bind = engine
>>> t = Table("test_ck", md, Column("id", Integer, primary_key=True),
... Column("id2", Integer, CheckConstraint("id2 between 1 and 10", name="my_constraint")))
>>> md.create_all(engine)
2011-10-21 19:53:45,523 INFO sqlalchemy.engine.base.Engine select version()
2011-10-21 19:53:45,523 INFO sqlalchemy.engine.base.Engine {}
2011-10-21 19:53:45,708 INFO sqlalchemy.engine.base.Engine select current_schema()
2011-10-21 19:53:45,709 INFO sqlalchemy.engine.base.Engine {}
2011-10-21 19:53:45,804 INFO sqlalchemy.engine.base.Engine select relname from pg_class c join pg_namespace n on n.oid=c.relnamespace where n.nspname=current_schema() and relname=%(name)s
2011-10-21 19:53:45,804 INFO sqlalchemy.engine.base.Engine {'name': u'test_ck'}
2011-10-21 19:53:46,168 INFO sqlalchemy.engine.base.Engine
CREATE TABLE test_ck (
id SERIAL NOT NULL,
id2 INTEGER CHECK (id2 between 1 and 10),
PRIMARY KEY (id)
)


2011-10-21 19:53:46,168 INFO sqlalchemy.engine.base.Engine {}
2011-10-21 19:53:47,116 INFO sqlalchemy.engine.base.Engine COMMIT
>>>

Michael Bayer

unread,
Oct 21, 2011, 8:09:39 PM10/21/11
to sqlal...@googlegroups.com
Because not every DB supports this (such as MySQL), we have not yet implemented the feature of named column level constraints across the board yet. We'd have to either implement it only for those DBs which support it, or add exceptions to those which don't. The main job is a. figure out which backends support it, and b. write some tests.

Here's a patch:

http://www.sqlalchemy.org/trac/ticket/2305

Table-level CHECK constraints do support generation of the "name" in all cases, however.

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

Mariano Mara

unread,
Oct 21, 2011, 8:16:29 PM10/21/11
to sqlal...@googlegroups.com
On 21.10.11 20:09, Michael Bayer wrote:
> Because not every DB supports this (such as MySQL), we have not yet implemented the feature of named column level constraints across the board yet. We'd have to either implement it only for those DBs which support it, or add exceptions to those which don't. The main job is a. figure out which backends support it, and b. write some tests.
>
> Here's a patch:
>
> http://www.sqlalchemy.org/trac/ticket/2305
>
> Table-level CHECK constraints do support generation of the "name" in all cases, however.
>

Thanks a lot, the patch works like charm

Reply all
Reply to author
Forward
0 new messages