Re: [sqlalchemy] Child table with 2 relationships to parent

2019-05-30 Thread Simon King
I think everything you have said is correct. If you use "relationship" to create a link from class A to class B, then "backref" is just a shortcut to create a corresponding relationship in the other direction, from B to A. They are entirely optional; you don't have to create a backref if you don't

Re: [sqlalchemy] Child table with 2 relationships to parent

2019-05-29 Thread Desmond Lim
Thanks Simon for your help. I have been playing around with the backref and I think I understand it, I just want to clarify my understanding before "close" this question. 1. There is no requirements to set a backref to any table, it could be the main table or the table that contains the

Re: [sqlalchemy] Child table with 2 relationships to parent

2019-05-29 Thread Simon King
If you're using backrefs, it doesn't really matter which end of the relationship you configure. In the example above, it would be just as legitimate to remove the source_node and target_node relationship definitions, and define them on the NodesModel instead: class NodesModel(Base):

Re: [sqlalchemy] Child table with 2 relationships to parent

2019-05-29 Thread Desmond Lim
Hi Simon, Thanks for the help just a follow up to clarify this. Does this mean if I place the backref in the relationships definition in the Relationships model, this works the same way as the backref in a parent model? I'm asking because backref have all been placed in the parent models and for

Re: [sqlalchemy] Child table with 2 relationships to parent

2019-05-29 Thread Simon King
foreign_keys and backref are different concepts. foreign_keys is a hint to SQLAlchemy on how to create the join condition between 2 classes. backref specifies a property that should be created on the other end of the relationship to allow you to follow the relationship in the other direction. For

Re: [sqlalchemy] Child table with 2 relationships to parent

2019-05-29 Thread Desmond Lim
Hi there, Sorry, I've actually found the solution after I've posted my question again. But I have to ask. I'm doing this in my relationships model: source_node = relationship("NodesModel", foreign_keys=[source_node_id]) target_node = relationship("NodesModel", foreign_keys=[target_node_id])

Re: [sqlalchemy] Child table with 2 relationships to parent

2019-05-29 Thread Desmond Lim
Hi Simon, I've read and I've tried a number of what is written but I still can't solve it. I've done this: class RelationshipsModel(db.Model): __tablename__ = 'relationships' source_node_id = db.Column(db.BigInteger, db.ForeignKey('nodes.id'), primary_key=True) target_node_id =

Re: [sqlalchemy] Child table with 2 relationships to parent

2019-05-29 Thread Simon King
On Wed, May 29, 2019 at 10:08 AM Desmond Lim wrote: > > Hi there, > > I'm been puzzling over this and still can't find answer. > > I have 2 tables: > > Nodes: > > class NodesModel(db.Model): > __tablename__ = 'nodes' > > id = db.Column(db.BigInteger, primary_key=True) > project_uuid =