2
當預先加載一個孩子的關係,我怎麼能只加載父模型的幾列:SQLAlchemy的LOAD_ONLY對父模型
這工作,如果我只需要chapters
模式title
列:
session.query(Book)\
.options(joinedload('chapters').load_only('title'))
但是,這將引發一個錯誤:
session.query(Book.author)\
.options(joinedload('chapters').load_only('title'))
Query has only expression-based entities - can't find property named 'chapters'.
換句話說,我婉噸ORM語法這個SQL:
SELECT
books.author,
chapters.title,
FROM
books JOIN chapters ON book.id = chapters.book_id
非常感謝你。 – norbertpy