2015-08-14 108 views
3

假設我有以下的(在Python 3和SQLAlchemy的):如何查詢SQLAlchemy對象實例中的一對多關係?

class Book(Base): 
    id = Column(Integer, primary_key=True) 
    chapters = relationship("Chapter", backref="book") 

class Chapter(Base): 
    id = Column(Integer, primary_key=True) 
    name = Column(String) 
    book_id = Column(Integer, ForeignKey(Book.id)) 

def check_for_chapter(book): 
    # This is where I want to check to see if the book has a specific chapter. 
    for chapter in book.chapters: 
     if chapter.name == "57th Arabian Tale" 
      return chapter 
    return None 

這感覺就像是「非習慣」的方法,因爲它似乎不太可能利用數據庫來搜索給定的一章。在最糟糕的情況下,看起來好像n調用db會檢查章節標題,儘管我對SQLAlchemy的有限理解表明這可以在周圍進行配置。我不知道的是,如果有一種方法可以直接針對您已經獲取的對象的關係直接發起查詢,如果是這樣,那麼怎麼做呢?

+1

爲什麼不查詢'book.id'的'Chapter'表?這將只需要一個查詢 – goodcow

+0

請注意,這是一個簡化的情況:可能很簡單,啓動第二個獨立查詢是此信息最快/最好的方式。但是a)原始對象被檢查了各種安全問題,並且b)它可能已經將這些信息放入緩存中,並且我不確定是否單獨的查詢將必然繞過原始對象的任何緩存。 –

回答

1

如果你想獲得特定圖書的特定章節,下面的代碼應該這樣做在一個SQL語句:

book = ... # get book instance 

chapter = (
    session.query(Chapter) 
    .with_parent(book) 
    .filter(Chapter.name == "57th Arabian Tale") 
    .one() 
) 

如果,例如,你只有書名和章節標題,你可以這樣做:

chapter = (
    session.query(Chapter) 
    .join(Book) 
    .filter(Book.name == "One Thousand and One Nights") 
    .filter(Chapter.name == "57th Arabian Tale") 
    .one() 
) 

又看了Querying with JoinsSQLAlchemy Documentation休息。

相關問題