07-20-2023, 05:05 AM
Can anyone explain the concepts of these two ideas and how they relate to making relationships between tables? I can't really seem to find anything that explains it clearly and the documentation feels like there's too much jargon to understand in easy concepts. For instance, in this example of a one to many relationship in the documentation:
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
children = relationship("Child", back_populates="parent")
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))
parent = relationship("Parent", back_populates="children")
Why does the `relationship()` go inside the parent class while `ForeignKey` goes inside the child class? And what does having `back_populates` exactly do to one another? Does having the placement of which class the `relationship()` function exist in matter?
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
children = relationship("Child", back_populates="parent")
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))
parent = relationship("Parent", back_populates="children")
Why does the `relationship()` go inside the parent class while `ForeignKey` goes inside the child class? And what does having `back_populates` exactly do to one another? Does having the placement of which class the `relationship()` function exist in matter?