[sqlalchemy] Re: Help - Two relations to same table

2008-07-22 Thread Michael Bayer


On Jul 22, 2008, at 1:48 PM, Erez wrote:


 So how would you solve it?

 I tried defining two foriegn keys:
 class Link(Base):
   __tablename__ = 'links'

   node_id = Column(Integer, ForeignKey('nodes.id'))
   node_id2 = Column(Integer, ForeignKey('nodes.id'))

   id = Column(Integer, primary_key=True)
   type = Column(String)

   fro = relation(Node, order_by=node_id, backref=links_out)
   to = relation(Node, order_by=node_id2, backref=links_in)

 but got:
 sqlalchemy.exc.ArgumentError: Could not determine join condition
 between parent/
 child tables on relation Link.fro.  Specify a 'primaryjoin'
 expression.  If this
 is a many-to-many relation, 'secondaryjoin' is needed as well.

 Is a primaryjoin necessary then?

yup


 I've tried to look-up the solution, but couldn't find any.

here 
http://www.sqlalchemy.org/docs/05/mappers.html#advdatamapping_relation_customjoin



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Help - Two relations to same table

2008-07-22 Thread Erez


On Jul 22, 4:51 pm, Michael Bayer [EMAIL PROTECTED] wrote:
 On Jul 22, 2008, at 6:50 AM, Erez wrote:







  Hi,
  I apologize in advance if this is a newbie question, but this is
  pretty wierd and I couldn't find an answer in the docs.

  I have these two tables:

  class Node(Base):
     __tablename__ = 'nodes'

     id = Column(Integer, primary_key=True)
     name = Column(String)

  class Link(Base):
     __tablename__ = 'links'

     node_id = Column(Integer, ForeignKey('nodes.id'))

     id = Column(Integer, primary_key=True)
     type = Column(String)
     fro = relation(Node, order_by=Node.id, backref=links_out)
     to = relation(Node, order_by=Node.id, backref=links_in)

  Just to clarify, I want each link to appear in the links_out of its
  from-node, and in the links_in of it's to-node.

  This works just fine when I create the classes, but once I commit the
  changes into a session, everything gets messed up (maybe the links_in
  and links_out aren't seperated as I would expect).

  A quick example:
  sqlalchemy.__version__
  '0.5.0beta2'
  cat = Node()
  cat.name = cat
  animal = grm.Node()
  animal.name = animal
  link = Link()
  link.type = is a
  link.fro = cat
  link.to = animal
  link
  #cat is a #animal
  session.add(cat)
  session.add(animal)
  session.add(link)
  session.commit()
  link
  #animal is a #animal

 you have only one foreign key to the nodes table, but two  
 relations.  How can a single row in links maintain two separate  
 references to both fro and to ?


So how would you solve it?

I tried defining two foriegn keys:
class Link(Base):
__tablename__ = 'links'

node_id = Column(Integer, ForeignKey('nodes.id'))
node_id2 = Column(Integer, ForeignKey('nodes.id'))

id = Column(Integer, primary_key=True)
type = Column(String)

fro = relation(Node, order_by=node_id, backref=links_out)
to = relation(Node, order_by=node_id2, backref=links_in)

but got:
sqlalchemy.exc.ArgumentError: Could not determine join condition
between parent/
child tables on relation Link.fro.  Specify a 'primaryjoin'
expression.  If this
 is a many-to-many relation, 'secondaryjoin' is needed as well.

Is a primaryjoin necessary then?
I've tried to look-up the solution, but couldn't find any.

Thanks!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Help - Two relations to same table

2008-07-22 Thread Michael Bayer


On Jul 22, 2008, at 6:50 AM, Erez wrote:


 Hi,
 I apologize in advance if this is a newbie question, but this is
 pretty wierd and I couldn't find an answer in the docs.

 I have these two tables:

 class Node(Base):
   __tablename__ = 'nodes'

   id = Column(Integer, primary_key=True)
   name = Column(String)


 class Link(Base):
   __tablename__ = 'links'

   node_id = Column(Integer, ForeignKey('nodes.id'))

   id = Column(Integer, primary_key=True)
   type = Column(String)
   fro = relation(Node, order_by=Node.id, backref=links_out)
   to = relation(Node, order_by=Node.id, backref=links_in)


 Just to clarify, I want each link to appear in the links_out of its
 from-node, and in the links_in of it's to-node.

 This works just fine when I create the classes, but once I commit the
 changes into a session, everything gets messed up (maybe the links_in
 and links_out aren't seperated as I would expect).

 A quick example:
 sqlalchemy.__version__
 '0.5.0beta2'
 cat = Node()
 cat.name = cat
 animal = grm.Node()
 animal.name = animal
 link = Link()
 link.type = is a
 link.fro = cat
 link.to = animal
 link
 #cat is a #animal
 session.add(cat)
 session.add(animal)
 session.add(link)
 session.commit()
 link
 #animal is a #animal


you have only one foreign key to the nodes table, but two  
relations.  How can a single row in links maintain two separate  
references to both fro and to ? 
  

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---