2011-05-19 27 views
1

我有兩個表項和ItemDescriptions如何根據SqlAlchemy中的表B選擇表A中的一個?

class Item(Base): 
    __tablename__ = "item" 
    id = Column(Integer, primary_key=True) 
    description_id = Column(Integer, ForeignKey('item_description.id')) 

class ItemDescription(Base): 
    __tablename__ = "item_description" 
    id = Column(Integer, primary_key=True) 

鑑於ItemDescriptions我想這樣的項目有每個ItemDescription ID的一個項目的名單列表。我不在乎哪個項目。

[編輯爲清晰起見]

鑑於項目和說明的這個名單:

Item, Description 
1 , 1 
2 , 1 
3 , 1 
4 , 2 
5 , 2 
6 , 3 
7 , 3 
8 , 3 

我想要一個查詢,將返回類似:

Item, Description 
2 , 1 
4 , 2 
7 , 3 

我無法完成子查詢等。

謝謝你的幫助

回答

2

我是column_property的忠實粉絲。這裏有一種方法可以用column_property來做你想做的事情:

from sqlalchemy import * 
from sqlalchemy.orm import * 
from sqlalchemy.ext.declarative import declarative_base 

Base = declarative_base() 

class Item(Base): 
    __tablename__ = 'item' 

    id = Column(Integer, primary_key=True) 
    description_id = Column(Integer, ForeignKey('item_description.id')) 

class ItemDescription(Base): 
    __tablename__ = 'item_description' 

    id = Column(Integer, primary_key=True) 

    any_item_id = column_property(
     select(
      [Item.id], 
      id == Item.description_id, 
      limit=1, 
     ).label('any_item_id'), 
     deferred=True, 
    ) 

e = create_engine('sqlite://', echo=True) 
Base.metadata.create_all(e) 

s = Session(e) 

descriptions = [ 
    ItemDescription(id=1), 
    ItemDescription(id=2), 
    ItemDescription(id=3), 
] 

s.add_all(descriptions) 

items = [ 
    Item(id=1, description_id=1), 
    Item(id=2, description_id=1), 
    Item(id=3, description_id=1), 
    Item(id=4, description_id=2), 
    Item(id=5, description_id=2), 
    Item(id=6, description_id=3), 
    Item(id=7, description_id=3), 
    Item(id=8, description_id=3), 
] 

s.add_all(items) 

query = s.query(ItemDescription).options(undefer('any_item_id')) 
for description in query: 
    print description.any_item_id, description.id 

# alternative way without using column_property 
query = s.query(
    select(
     [Item.id], 
     ItemDescription.id == Item.description_id, 
     limit=1, 
    ).label('any_item_id'), 
    ItemDescription, 
) 
for item_id, description in query: 
    print item_id, description.id 
+0

column_property看起來不錯,謝謝!如果我想把它作爲單個查詢而不是列屬性,我會把select放在子查詢中嗎? – Ben 2011-05-20 21:11:50

+0

是的,你可以將選擇移動到查詢中。我已經添加了答案。就個人而言,我仍然更喜歡column_property :) – sayap 2011-05-21 01:20:12

+0

我喜歡column_query方法,但也感謝您以查詢方式顯示。 – Ben 2011-05-24 17:50:48

0
from sqlalchemy.orm import relationship, backref 

class Item(Base): 
    __tablename__ = "item" 
    id = Column(Integer, primary_key=True) 
    description_id = Column(Integer, ForeignKey('item_description.id')) 
    desc = relationship(User, backref=backref('desc', order_by=id)) 

class ItemDescription(Base): 
    __tablename__ = "item_description" 
    id = Column(Integer, primary_key=True) 

Now your every ItemDescription class will have an backref called `desc` which is nothing but a list of Item. 

Now you can do something like this 

item_desc = session.query(ItemDescription).\ 
...      options(joinedload('desc').all() 

for item in item_desc: 
    print item.desc 

我想這可能不會給你確切的答案。它會幫助我猜

+0

對不起,我的解釋不是很清楚,我會編輯它以使其更清晰。 – Ben 2011-05-19 22:41:40

相關問題