尝试以这种方式使用sqlalchemy.schema.CheckConstraint:
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'"))
(意味着标签表上的多对多自引用关系,并且主题是具有预定义前缀的标签,显然),最终结果
...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'
我在哪里错了?在这种情况下是否可能有这样的约束?
当前的后端是sqlite,但我考虑将来迁移到Postgres,所以寻找一个更不通用的收据。
更新:sqlalchemy 0.7.5
答案 0 :(得分:1)
嗯,这是我的错,实际上是基于错误的错误信息。事实证明,实际错误发生在前一行,即正确的语法是:
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'"))
在PrimaryKeyConstraint中注意列表定义之前的星号。