0

Trying to use sqlalchemy.schema.CheckConstraint in this way:

themes2tags_table = Table('themes2tags', Base.metadata,
 Column('theme_id', String(32), ForeignKey('tags.id')),
 Column('tag_id', String(32), ForeignKey('tags.id')),
 PrimaryKeyConstraint(['theme_id', 'tag_id']),
 CheckConstraint("substr(theme_id,1,1)='3'"))

(meant many-to-many self-referential relationship over tags table and that themes are tags with predefined prefix, obviously), what ends up with

 ...CheckConstraint("substr(theme_id,1,1)='3'")
 File "build\bdist.win32\egg\sqlalchemy\schema.py", line 303, in __new__
 File "build\bdist.win32\egg\sqlalchemy\schema.py", line 370, in _init
 File "build\bdist.win32\egg\sqlalchemy\schema.py", line 64, in _init_items
 File "build\bdist.win32\egg\sqlalchemy\events.py", line 234, in _set_parent_wi
th_dispatch
 File "build\bdist.win32\egg\sqlalchemy\schema.py", line 2133, in _set_parent
 File "build\bdist.win32\egg\sqlalchemy\schema.py", line 1909, in _set_parent
 File "build\bdist.win32\egg\sqlalchemy\schema.py", line 1883, in _set_parent
 File "build\bdist.win32\egg\sqlalchemy\sql\expression.py", line 2213, in add
AttributeError: 'list' object has no attribute 'key'

Where am I wrong here? Is it possible to have such a constraint in these circumstances?

Current backend is sqlite, but I consider migrating to Postgres in the future, so looking for a more-less generic receipt.

Upd: sqlalchemy 0.7.5

asked Jul 31, 2012 at 8:36
1
  • What version of SQLAlchemy is this? This is not 0.7.8, in any case. Commented Jul 31, 2012 at 9:03

1 Answer 1

1

Well, that was my fault, in fact, based on wrong error message, however. As it turned out, the actual error was on the previous line, i.e. the correct syntax is:

themes2tags_table = Table('themes2tags', Base.metadata,
 Column('theme_id', String(32), ForeignKey('tags.id')),
 Column('tag_id', String(32), ForeignKey('tags.id')),
 PrimaryKeyConstraint(*['theme_id', 'tag_id']),
 CheckConstraint("substr(theme_id,1,1)='3'"))

Pay attention to that asterisk before list definition in PrimaryKeyConstraint.

answered Aug 1, 2012 at 7:48

1 Comment

A simpler way to write that would be PrimaryKeyConstraint('theme_id', 'tag_id').

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.