如何在sqlalchemy中获取关系列的数据?

yrwegjxp  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(168)

我在sqlite中有两个表,如下所示:

class RoomDetail(Base):
    __tablename__ = 'room_detail'
    __table_args__ = (
        UniqueConstraint('roomid', 'bedid'),
    )

    id = Column(Integer, primary_key=True)
    roomid = Column(ForeignKey('room.id', ondelete='CASCADE'))
    bedid = Column(ForeignKey('bed.id', ondelete='CASCADE'))
    quantity = Column(Integer)

    bed = relationship('Bed')
    room = relationship('Room')
class Room(Base):
    __tablename__ = 'room'

    id = Column(Integer, primary_key=True)
    price = Column(Float)
    room_number = Column(String)

    bookings = relationship('Booking',back_populates='room')
    roomdetails = relationship('RoomDetail',back_populates='room')

让我们关注一下字段之间的关系 room 和场 roomdetails 然后我创建了一个模式 Room 例如:

class Room(BaseModel):
    price :Optional[float] = Field(None)
    room_number :Optional[str] = Field(None)

    class Config:
        orm_mode = True

class RoomWithListRoomDetail(Room):
    id:int
    roomdetails:List[RoomDetailDB]

    class Config:
        orm_mode = True

当然,我已经创建了模式 RoomDetailDB .
之后,我做了如下测试:

rooms:RoomWithListRoomDetail = db.query(database.Room).all()

我得到的结果包括 id,price,room_number 但我不能得到这个领域 roomdetails .有什么想法吗 roomdetails 为了我?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题