Can't compare datetime in Postgresql sqlalchemy.exc.ProgrammingError ProgrammingError: (ProgrammingError) can't adapt type 'time.struct_time'

StackOverflow https://stackoverflow.com/questions/22332406

Вопрос

I have the following database structure using SQLAlchemy using Postgresql with Flask:

class Attendance(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  time = db.Column(db.DateTime(timezone=True))

When I get a date from the user and try and query it against the date of the models:

from_date = time.strptime(request.args['from_attendance'],'%m/%d/%Y')
to_date = time.strptime(request.args['to_attendance'],'%m/%d/%Y')
dates = Attendance.query.filter(Attendance.time < from_date,Attendance.time > to_date).all()

I get the following error:

sqlalchemy.exc.ProgrammingError
ProgrammingError: (ProgrammingError) can't adapt type 'time.struct_time' 'SELECT attendance.id     AS attendance_id, attendance.time AS attendance_time, attendance.status AS attendance_status,........

I believe this is a postgresql specific issue, but I am really at a loss for words.

Это было полезно?

Решение

Datetime columns in Flask-SQLAlchemy are datetime objects. You're using time module to parse.

Try:

from_date = datetime.datetime.strptime(request.args['from_attendance'],'%m/%d/%Y')
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top