lundi 1 février 2016

Using a Datetime Restriction in SQLAlchemy with SQLite Backend

I am trying to use a query restriction by date with SQL alchemy. Example code below:

def young_puppies(session=get_session()):
    """Query all of the puppies.

    Return those that are less than 6 months old organized by
    the youngest first.
    """
    young = []
    today = datetime.date.today()
    sixm = today - relativedelta(months=-6)
    sel = session.query(Puppy).\
        filter(Puppy.dateOfBirth.between(today, sixm)).\
        order_by(desc(Puppy.dateOfBirth))
    print (sel)
    res = session.execute(sel)
    for row in res:
        young.append(dict(row))
    session.close
    return young

However, the query generation is exhibiting unexpected behavior:

SELECT puppy.name AS puppy_name, puppy.id AS puppy_id, puppy."dateOfBirth" AS "puppy_dateOfBirth", puppy.gender AS puppy_gender, puppy.picture AS puppy_picture, puppy.weight AS puppy_weight, puppy.shelter_id AS puppy_shelter_id 
FROM puppy 
WHERE puppy."dateOfBirth" BETWEEN ? AND ? ORDER BY puppy."dateOfBirth" DESC

The same behavior is exhibited even if I cast the type to str before inserting into the filter. I am not sure why this is occurring or if there is a workaround. This should work according to the research I've done. The ? instead of the date in the WHERE restriction is the question.

Aucun commentaire:

Enregistrer un commentaire