SQLite和PostgreSQL区间兼容性

2024-04-27 20:29:35 发布

您现在位置:Python中文网/ 问答频道 /正文

试图让一个查询同时在PostgreSQL和SQLite上工作-我需要该查询通过使用数据库列添加iterval来比较datetime

end_date = date + timedelta(minutes=duration)
appointment_end_date = Appointment.date + func.make_interval(
    0, 0, 0, Appointment.duration
)
existing_lessons = Appointment.query.filter(
        or_(
            and_(Appointment.date <= end_date, Appointment.date >= date),
            and_(appointment_end_date >= date, appointment_end_date <= end_date),
        )
).all()

查询试图查找date变量位于Appointment.dateAppointment.date + Appointment.duration内的任何行。如果我可以用timedelta来写,我会写以下内容:

Appointment.date + timedelta(minutes=Appointment.duration)

目前只适用于PostgreSQL:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such function: make_interval

有没有可能对这两个数据库进行这样的查询?你知道吗


Tags: and数据库sqlitedatemakepostgresqltimedeltaend