0

尝试以这种方式使用 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

4

1 回答 1

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 中列表定义之前的星号。

于 2012-08-01T07:48:46.487 回答