I have 2 SqlAlchemy entities: study and series with 1 to many relation (study has many series). I crearted a relationship that way:
class SeriesDBModel(Base):
__tablename__ = "series"
series_uid = Column(String, primary_key=True)
study_uid = Column(String, ForeignKey(StudyDBModel.study_uid))
study = relationship("StudyDBModel", lazy="subquery", cascade="all")
class StudyDBModel(Base):
__tablename__ = "study"
study_uid = Column(String, primary_key=True)
My question is: Do I have to create the entities like this:
series = SeriesDBModel()
study = StudyDBModel()
series.study = study
Or
Can I just create both entities independently but with the same study_uid? I'm asking because the second way i how I save my entities but using series.study returns None.
BTW, it's not a "sessio.commit()" issue. I'm verifing both entities exist in the DB before trying series.study..
I tried to fetch series.study as above.
source https://stackoverflow.com/questions/75289115/create-non-strait-forawrd-sqlalchemi-relationship
Comments
Post a Comment