'sqlalchemy: add new "Parent" to Parent Table, which should link to multiple "Children" in (existing) Children Table
child_I have a Table "ChildTable". I want to create a new table "ParentTable", and continue adding new entries to "Parent Table". Each Parent has multiple children, so the new entry in "ParentTable" should link to multiple existing Children in the ChildrenTable. How can I do this?
Firstly, is the way I have set up my classes for this correct?
class Child(Base):
__tablename__ = "child"
child_id = Column(String, primary_key=True)
child_eyecolor = Column(String)
parents = relationship('Parent', backref='child_id', lazy='dynamic')
class Parent(Base):
__tablename__ = "parent"
parent_name = Column(String, unique=True, primary_key=True, nullable=False)
children = Column(String, ForeignKey('child.child_id'))
I have gone through the sqlalchemy documentation, but it is a bit difficult to follow as I'm new to the field, and there aren't too many examples. I also searched StackOverflow, where I found many answers that explain the reverse situation, i.e. a scenario where a "ParentTable" exists, each Parent has multiple Children, and a new "Child" is added. All the one-to-many, many-to-one, and many-to-many examples I've seen aren't working for me
Could anyone please point me to an example, or a relevant question if this has already been solved somewhere else? Please let me know if I've misunderstood something. I would appreciate it very much!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
