2010-04-12 16 views
5

मैं स्क्लेक्लेमी, या यहां तक ​​कि डेटाबेस प्रोग्रामिंग के लिए काफी नया हूं, शायद मेरा प्रश्न बहुत आसान है। अब मैं दो वर्ग/तालिका है:घोषणात्मक शैली के स्टार्टर प्रश्न SQLAlchemy संबंध()

class User(Base): 
    __tablename__ = 'users' 
    id = Column(Integer, primary_key=True) 
    name = Column(String(40)) 
    ... 

class Computer(Base): 
    __tablename__ = 'comps' 
    id = Column(Integer, primary_key=True) 
    buyer_id = Column(None, ForeignKey('users.id')) 
    user_id = Column(None, ForeignKey('users.id')) 
    buyer = relation(User, backref=backref('buys', order_by=id)) 
    user = relation(User, backref=backref('usings', order_by=id)) 

बेशक, यह नहीं चल सकता।

File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/state.py", line 71, in initialize_instance 
    fn(self, instance, args, kwargs) 
    File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/mapper.py", line 1829, in _event_on_init 
    instrumenting_mapper.compile() 
    File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/mapper.py", line 687, in compile 
    mapper._post_configure_properties() 
    File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/mapper.py", line 716, in _post_configure_properties 
    prop.init() 
    File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/interfaces.py", line 408, in init 
    self.do_init() 
    File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/properties.py", line 716, in do_init 
    self._determine_joins() 
    File "/Library/Python/2.6/site-packages/SQLAlchemy-0.5.8-py2.6.egg/sqlalchemy/orm/properties.py", line 806, in _determine_joins 
    "many-to-many relation, 'secondaryjoin' is needed as well." % (self)) 
sqlalchemy.exc.ArgumentError: Could not determine join condition between parent/child tables on relation Package.maintainer. Specify a 'primaryjoin' expression. If this is a many-to-many relation, 'secondaryjoin' is needed as well. 

कक्षा कंप्यूटर में दो विदेशी कुंजी है, तो संबंध() कॉलिंग्स निर्धारित नहीं कर सकता जो एक प्रयोग किया जाना चाहिए: यह पश्व-अनुरेखन है। मुझे लगता है कि मुझे इसे निर्दिष्ट करने के लिए अतिरिक्त तर्कों का उपयोग करना चाहिए, है ना? और कैसे? धन्यवाद

उत्तर

10

सही सिंटैक्स होना चाहिए:

buyer = relation(User, backref=backref('buys', order_by=id)) 
user = relation(User, backref=backref('usings', order_by=id)) 

पी.एस. अगली बार कृपया निर्दिष्ट करें कि ट्रेसबैक पोस्ट करके "रन नहीं" करके आपका क्या मतलब है।

अद्यतन: अद्यतन सवाल में ट्रैस बैक का कहना है कि वास्तव में आप क्या जरूरत है:

buyer = relation(User, primaryjoin=(buyer_id==User.id), 
       backref=backref('buys', order_by=id)) 
user = relation(User, primaryjoin=(user_id==User.id), 
       backref=backref('usings', order_by=id)) 
+0

आपकी सलाह के लिए धन्यवाद: primaryjoin शर्त निर्दिष्ट। टाइपो को ठीक किया गया है, और बैकट्रैक जोड़ा गया है। – jfding

+0

धन्यवाद, समस्या हल हो गई। – jfding

संबंधित मुद्दे