I have a many to one relationship between two SQL tables using SQLAlchemy. For example:
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
child_id = Column(Integer, ForeignKey('child.id'))
child = relationship("Child")
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
name = Column(String(100))
What I would like to be able to do is be able to add information from the Child class to the parent. I tried a join query:
result = session.query(Parent).join(Child).all()
While this query adds the appropriate Child object to the Parent object at parent.child it only returns the first parent for each child, i.e. I have four parents and two children in my database and this query only returns parents 1 and 3. How do I fix the query to return all four parents? The second I have is if I wanted to just add the child's name to the parent, not the entire child object, as parent.child_name how would I go about doing that?
outerjoin:session.query(Parent).outerjoin(Child).all()Still not sure about the second question. - Michael