2012-01-05 56 views
2

在Rails中,你可以設置關聯,這樣,如果你有一個文章和類別模型和「分類」是的「文章」一欄,你可以這樣做:可能在SQLAlchemy中設置類似rails的關聯?

article.category --> returns the category Object 

但在SQLAlchemy的,我只能做:

article.category --> only returns the category's id 

有沒有辦法使用SQLAlchemy獲取Category對象?我目前使用定義模型的聲明式風格。

回答

6

您可以創建一個像

category_id = Column(Integer, ForeignKey('category.id')) 
category = relationship('Category') 

這樣,當你訪問類,你會得到對象關係。

+1

謝謝,我結束了這麼做! – trinth 2012-01-06 17:24:49

+3

我感謝你發佈一個答案,而不是隻是用文件來批評提問者。 – 2012-05-17 23:20:39