You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The problem is in accessing attributes after tables join.
So my models are next:
class Itinerary(Base):
__tablename__ = 'itineraries'
id = Column(Integer, primary_key=True)
name = Column(String(128))
bus_id = Column(Integer, ForeignKey('buses.id'), nullable=False)
class Bus(Base):
__tablename__ = 'buses'
id = Column(Integer, primary_key=True)
name = Column(String(128), nullable=False)
number_plate = Column(String(16), nullable=False, unique=True, index=True)
The Pydantic models:
class ItinerarySchema(BaseModel):
id: inty
name: str
bus: BusSchema
class BusSchema(BaseModel):
id: int
name: str
number_plate: str
Also the function to get itinerary:
async def get_itinerary_by_id(db: Database, itinerary_id: int) -> ItinerarySchema:
query = select(Itinerary, Bus).join(Bus).filter(Itinerary.id == itinerary_id)
itinerary = await db.fetch_one(query=query)
bus = BusSchema(
id=itinerary.id_1, # <- HERE is the problem
name=itinerary.name_1, # <- HERE is the problem
number_plate=itinerary.number_plate,
)
return ItinerarySchema(id=itinerary['id'], name=itinerary['name'], bus=bus)
It's works, but looks weird. Could you pls describe how I can access to the attributes in a more right way than *.id_1 and *.name_1
The text was updated successfully, but these errors were encountered:
The problem is in accessing attributes after tables join.
So my models are next:
The Pydantic models:
Also the function to get itinerary:
It's works, but looks weird. Could you pls describe how I can access to the attributes in a more right way than *.id_1 and *.name_1
The text was updated successfully, but these errors were encountered: