我試圖在與the example in the docs非常類似的情況下使用關聯代理的標記。這裏是我的架構(這是一個博客)的一個子集,使用聲明:爲什麼SQLAlchemy/associationproxy複製我的標籤?
class Tag(Base):
__tablename__ = 'tags'
id = Column(Integer, primary_key=True)
tag = Column(Unicode(255), unique=True, nullable=False)
class EntryTag(Base):
__tablename__ = 'entrytags'
entry_id = Column(Integer, ForeignKey('entries.id'), key='entry', primary_key=True)
tag_id = Column(Integer, ForeignKey('tags.id'), key='tag', primary_key=True)
class Entry(Base):
__tablename__ = 'entries'
id = Column(Integer, primary_key=True)
subject = Column(Unicode(255), nullable=False)
# some other fields here
_tags = relation('Tag', backref='entries', secondary=EntryTag.__table__)
tags = association_proxy('_tags','tag')
以下是我想要使用它:
>>> e = db.query(Entry).first()
>>> e.tags
[u'foo']
>>> e.tags = [u'foo', u'bar'] # really this is from a comma-separated input
db.commit()
Traceback (most recent call last):
[...]
sqlalchemy.exc.IntegrityError: (IntegrityError) duplicate key value violates unique constraint "tags_tag_key"
'INSERT INTO tags (id, tag) VALUES (%(id)s, %(tag)s)' {'tag': 'bar', 'id': 11L}
>>> map(lambda t:(t.id,t.tag), db.query(Tag).all())
[(1, u'foo'), (2, u'bar'), (3, u'baz')]
ID爲2已經存在的標籤u'bar'
;爲什麼SQLAlchemy沒有附加那個而不是試圖創建它呢?某種程度上我的模式錯了嗎?
那麼,我鏈接的例子似乎暗示它會起作用。至少,對於他們來說,定義這個不錯的代理將標籤呈現爲字符串列表似乎很愚蠢,只能說我應該在使用它之前手動查找標籤ID。 他們的例子有一個奇怪的地方,它實際上並沒有'Tag.tag'上的唯一約束。也許他們的例子只是以很多重複標籤結束,在這一點上,我可能會拋棄「標籤」對象,並直接在「EntryTag」中標記標籤。但後來我失去了擁有不錯的backref'entries'的能力,對吧? – 2009-12-14 11:01:58
其實你是對的。他們甚至把它放在他們的常見問題解答中:http://www.sqlalchemy.org/trac/wiki/FAQ#Istherewayto自動創建一個獨特的關鍵字或其他關鍵詞,而不需要關鍵詞,並且可以在其中包含關鍵詞及其建議的修正:http://www.sqlalchemy。org/trac/wiki/UsageRecipes/UniqueObject – 2009-12-14 13:28:34
看着它,我同意他們的例子是不明確的,因爲你可能合理地期望相同的關鍵字解析爲相同的實例。如果他們選擇了第二張表格而不是唯一性的建議,會更好。 – Kylotan 2009-12-15 01:11:16