我想在peewee中加入多个表。不过,这个场景对我来说有点难弄清楚如何让它与peewee一起工作。
我有以下表格:
门票
TicketCategory类别
售票
事件
以下是我的模特:class TicketCategory(BaseModel):
venue_id = IntegerField()
name = CharField()
description = CharField()
class Ticket(BaseModel):
event = ForeignKeyField(Event)
category = ForeignKeyField(TicketCategory)
order_number = IntegerField()
tier_name = CharField()
num_available = IntegerField()
price = DecimalField()
class TicketBooking(BaseModel):
user_id = IntegerField()
ticket = ForeignKeyField(Ticket, related_name="ticketbookings")
price_paid = DecimalField()
created = DateTimeField()
deleted = DateTimeField()
class Event(BaseModel):
venue_id = IntegerField()
date = DateField()
event_image_url = CharField()
start = TimeField()
end = TimeField()
现在我想运行一个查询,它将选择给定用户的所有ticketbookings。运行我的加入后,我希望加载所有信息——我不希望在访问ticketbooking.ticket.category.name或ticketbooking.ticket.event.description时启动另一个查询
我不能这么做:return TicketBooking.select(TicketBooking, Ticket, TicketCategory, Event).join(Ticket).join(TicketCategory).join(Event).where(
TicketBooking.user_id == user_id,
TicketBooking.deleted >> None
)
因为事件不是TicketCategory上的外键,所以我得到一个错误。任何帮助都将不胜感激。