在SQLAlchemy中创建具有关系的对象的正确方法是什么

时间:2020-09-20 09:15:18

标签: sqlalchemy

我有两个模型,一个SQLite数据库上的Parent和Child。

$this->route('timeSlot');

我需要创建如下子代:

class Parent(Base): __tablename__ = 'PARENTS' id = Column(Integer, primary_key=True) name = Column(String(50), nullable=False, sqlite_on_conflict_unique='IGNORE') children = relationship("Child", back_populates="parent") class Child(Base): __tablename__ = 'CHILDREN' id = Column(Integer, primary_key=True) name = Column(String(50), nullable=False) parent_id = Column(Integer, ForeignKey('PARENTS.id'), nullable=False) parent = relationship("Parent", back_populates="children") , 那就是我要创建一个具有其名称和父级名称的子级。我想知道哪种是最好的方法。我是否可以指示SA去为我选择Parents,以便我从parent.name中检索parent.id?我可以利用某种缓存机制来不为每个添加的子对象查询父表(很少更改父表)。创建新孩子时,不需要创建新父母,但我希望将其链接到已经存在的父母。此外,我需要一长串要快速创建的子级列表(例如,为db播种)。

谢谢。

0 个答案:

没有答案
相关问题