2012-07-31 47 views
0

試圖以這種方式來使用sqlalchemy.schema.CheckConstraint:的SQLAlchemy: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,因此尋找更少的通用收據。

UPD:SQLAlchemy的0.7.5

+0

這是什麼版本的SQLAlchemy?無論如何,這不是0.7.8。 – 2012-07-31 09:03:05

回答

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列表定義之前星號。

+3

寫一個簡單的方法是PrimaryKeyConstraint('theme_id','tag_id')。 – 2012-08-01 08:01:19

+0

啊,當然,Wichert,對我很恥辱! ;) – 2012-08-02 11:58:21