2012-05-18 2 views
0

나는 placedBids이라는 사용자와 경매 사이에 many-many 테이블을 가지고 있습니다.쿼리 및 정보를 얻으십시오. 2 backrefs deep

나는 내가 경매에서 액세스 할 수 있습니다 또한 placedbids라고 placedBids에 역 참조 그래서 경매에서 질의하고, 다음 내가 예를 들어, 배치 입찰의 userid를 얻을 수 있습니다,하지만 난 정말 그것 username을 원하는, 그래서 내가 만든 userplacedbids 사이의 관계에있는 역 참조 (이 문제는 users라고하는 이미 다른 백 레퍼런스가 있기 때문에 변경되었습니다. 어쨌든 거기에 어떤 문제가 있습니다.) 새로운 것을 시도했습니다. sqlalchemy 많은 것을 시도해 보았습니다. userbids backref에서 열심히로드하는 것은 아무 소용이 없습니다.

이 내 마지막 쿼리입니다 :

auction = dbsession.query(Auction.id, Auction.endTime, Auction.currentPrice, Product.description, Product.title, User.username, PlacedBids.timeplaced, PlacedBids.userbids.username).outerjoin(Auction.placedbids).join(Auction.products).outerjoin(Auction.leader).filter(Auction.id == request.matchdict['id']).all() 

오류 : 나는 또한 outerjoin(Auction.placedbids.userbids) 또는 PlacedBids.userbids 등처럼에 합류 시도

2012-05-18T16:40:36+00:00 app[web.1]: AttributeError: Neither 'InstrumentedAttri bute' object nor 'Comparator' object has an attribute 'username'

...

난 몰라 sqlalchemy에 대한 많은 경험이 있습니다. 추가 문의는 usernameuser_id와 함께 할 수 있습니다. 문제는 없지만 그걸 만들지 않는 방법을 알고 싶습니다.

답변

0

조인은 항상 명시 적이어야하며 실제 상위 클래스의 측면에서 리드 엔터티 특성을 지정해야합니다. 관련 클래스 사이에는 "점선으로 된 다중 점"액세스가 없습니다. 이는 항상 별도로 테이트되어야하는 조인을 의미합니다.

PlacedBids.userbids이 UserBid의 인스턴스 여기에 가정 :

auction = dbsession.query(Auction.id, Auction.endTime, 
    Auction.currentPrice, Product.description, Product.title, User.username, 
    PlacedBids.timeplaced, UserBid.username).\ 
    outerjoin(Auction.placedbids).\ 
    join(Auction.products).\ 
    outerjoin(Auction.leader).\ 
    outerjoin(PlaceBids.userbids).\ 
    filter(Auction.id == request.matchdict['id']).all() 
+0

userbids이 사용자에 대한 역 참조가되고, 및 사용자에의 관계가 잘 보여주고 있음을 내가 이미 Auction.leader에서 User.username를 사용, 그렇다면 PlacedBids.userbids에서 액세스 한 다음 어쩌면 그 별칭의 별칭을 만들 수 있습니까? 감사합니다 –

+0

auction = dbsession.query (Auction.id, Auction.endTime, Auction.created, Auction.currentPrice, Product.description, Product.title, User.username, PlacedBids.timeplaced, PlacedBids.user_id, user_alias.username) .outerjoin (Auction.leader) .outerjoin (user_alias, PlacedBids.userbids) .filter (Auction.id == request.matchdict [ 'id']). all()가 작동했습니다. (Auction.placedbids) .join ! 어쨌든 고마워, 내가 훌륭한 아이디어를 제외하고 몇 가지 아이디어를 제외시켰다. –

관련 문제