我在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
为了我?
暂无答案!
目前还没有任何答案,快来回答吧!