fastapi sqlachemy function code example
Example: insert into relational database fastapi
def create_nested_family(db: Session, family: schemas.NestedFamilyCreate):
family_data = family.dict()
member_data = family_data.pop('members', None)
db_family = models.Family(**family_data)
db.add(db_family)
db.commit()
db.refresh(db_family)
family_id = db_family.id
for m in member_data:
m['family_id'] = family_id
db_member = models.Member(**m)
db.add(db_member)
db.commit()
db.refresh(db_member)
return db_family