python SQL Alchemy -使用NOT IN子句查询

vc9ivgsu  于 2022-12-10  发布在  Python
关注(0)|答案(1)|浏览(411)

SQL查询我想进入SQLAlchemy的形式。但我不能写查询与sqlalchemy框架。
查询:

SELECT * FROM Availability_Schedule 
WHERE Schedule_ID NOT IN (Select Schedule_ID FROM Appointments WHERE Status = 1) 
AND Staff_ID = 1

SQL模型:

class AvailabilitySchedule(db.Model):
__tablename__ = 'Availability_Schedule'

Schedule_ID = Column(INTEGER, primary_key=True)
Schedule_Date = Column(Date)
Slot_ID = Column(ForeignKey('Time_Slots.Slot_ID', ondelete='RESTRICT'), nullable=False, index=True)
Staff_ID = Column(ForeignKey('Hospital_Staff.Staff_ID'), nullable=False, index=True)
Room_ID = Column(ForeignKey('Rooms.Room_ID'), index=True)

Room = relationship('Room')
Time_Slot = relationship('TimeSlot')
Hospital_Staff = relationship('HospitalStaff')

class Appointment(db.Model):
__tablename__ = 'Appointments'

Appointment_ID = Column(INTEGER, primary_key=True, autoincrement=True)
Schedule_ID = Column(ForeignKey('Availability_Schedule.Schedule_ID'), index=True)
Status = Column(Integer)
Patient_ID = Column(ForeignKey('Patients.Patient_ID'), index=True)
Type = Column(String(255))

Patient = relationship('Patient')
Availability_Schedule = relationship('AvailabilitySchedule')

SQL炼金术:

schedules = AvailabilitySchedule.query.filter_by(
                                Staff_ID = doctorID,
                                Schedule_ID,not_in(Appointment.query.filter_by(Status = 1).all())
                                                         ).all()
xzabzqsa

xzabzqsa1#

这是你需要的两个技巧。

  1. Sqlalchemy提供了以下函数:.in_( ... )
    1.使用以下运算符处理求反:~

相关问题