2011-09-25 15 views
14
में एक ही मेज पर दो रिश्तों को परिभाषित करने के

मैं SQLAlchemy ट्यूटोरियल और अन्य इसी तरह के सवाल पर सभी देखा है, लेकिन मैं यह काम करने के लिए शामिल हो पाने के लिए संघर्ष कर रहा हो रहे हैं:कैसे SQLAlchemy

परिदृश्य: मैं मॉडल द्वारा प्रतिनिधित्व pages तालिका है। पेज उपयोगकर्ता द्वारा बनाया जा सकता है और किसी उपयोगकर्ता द्वारा संपादित किया जा सकता है, लेकिन जरूरी नहीं है। मेरे Page मॉडल की तरह इस (संक्षिप्त) दिखता है:

class Page(Base): 
    __tablename__ = 'pages' 

    id = Column(Integer, primary_key = True) 
    slug = Column(Text) 
    title = Column(Text) 
    direct_link = Column(Text) 
    body = Column(Text) 
    category_id = Column(Integer, ForeignKey('categories.id')) 
    published_on = Column(DateTime) 
    publishing_user_id = Column(Integer, ForeignKey('users.id')) 
    last_edit_on = Column(DateTime) 
    last_edit_user_id = Column(Integer, ForeignKey('users.id')) 

    # Define relationships 
    publish_user = relationship('User', backref = backref('pages', order_by = id), primaryjoin = "Page.publishing_user_id == User.id") 
    edit_user = relationship('User', primaryjoin = "Page.last_edit_user_id == User.id") 
    category = relationship('Category', backref = backref('pages', order_by = id)) 

मेरे उन User मॉडल के प्रतिनिधित्व वाले उपयोगकर्ताओं तालिका में जमा हो जाती है। जैसा कि मैंने कहा था कि मैं इस सबके लिए देख रहे स्क्लेक्लेमी दस्तावेज़ों में रहा हूं, मैंने इसे अपने उदाहरण के समान दिखने की कोशिश की है, लेकिन इसका कोई फायदा नहीं हुआ है। किसी भी तरह की सहायता का स्वागत किया जाएगा।

+0

इम यकीन है कि Ive यह सही क्या मैं यह करने के लिए चाहते हैं के लिए सेट अप नहीं पर प्रलेखन। मैं एक प्रश्न से एक पृष्ठ प्राप्त करने में सक्षम होना चाहता हूं, और प्रकाशन उपयोगकर्ता प्राप्त करने के लिए पेज.publish_user पर कॉल करना चाहता हूं। मैंने नीचे दिए गए सुझाव की कोशिश की, लेकिन अभी भी कोई भाग्य – richzilla

+0

आप कहते हैं कि यह काम नहीं कर रहा है, लेकिन क्या आप अधिक विशिष्ट हो सकते हैं - एक त्रुटि, एक अप्रत्याशित वापसी मूल्य, क्या? मुझे आपके द्वारा पोस्ट किए गए कोड में कोई स्पष्ट समस्या नहीं दिखाई दे रही है। – FMc

उत्तर

2

foreign_keys विकल्प का प्रयास करें:

publish_user = relationship(User, foreign_keys=publishing_user_id, 
            primaryjoin=publishing_user_id == User.id, 
            backref=backref('pages', order_by=id)) 
edit_user = relationship(User, foreign_keys=last_edit_user_id, 
           primaryjoin=last_edit_user_id == User.id) 
+1

जब आप '' विदेशी_की'' जोड़ते हैं और स्थिति में शामिल हो जाते हैं, तो 'primjoin'' विशेषताएँ जोड़ने की आवश्यकता नहीं होती है। – user2683246

7

मुझे लगता है कि आप लगभग यह अधिकार मिल गया; केवल Model नामों के बजाय primaryjoin को परिभाषित करते समय आपको Table नामों का उपयोग करना चाहिए। तो

# Define relationships 
publish_user = relationship('User', backref = backref('pages', order_by = id), 
    primaryjoin = "Page.publishing_user_id == User.id") 
edit_user = relationship('User', 
    primaryjoin = "Page.last_edit_user_id == User.id") 

उपयोग की बजाय:

# Define relationships 
publish_user = relationship('User', backref = backref('pages', order_by = id), 
    primaryjoin = "pages.publishing_user_id == users.id") 
edit_user = relationship('User', 
    primaryjoin = "pages.last_edit_user_id == users.id") 
9

संस्करण 0.8 के रूप में, SQLAlchemy relationship को अस्पष्ट केवल foreign_keys कीवर्ड पैरामीटर का उपयोग करके शामिल हल कर सकते हैं।

publish_user = relationship(User, foreign_keys=[publishing_user_id], 
            backref=backref('pages', order_by=id)) 
edit_user = relationship(User, foreign_keys=[last_edit_user_id]) 

http://docs.sqlalchemy.org/en/rel_0_9/orm/join_conditions.html#handling-multiple-join-paths