ponyorm / pony

Pony Object Relational Mapper
Apache License 2.0
3.58k stars 243 forks source link

select by day (unix timestamp) #360

Open hubcin opened 6 years ago

hubcin commented 6 years ago

How do I translate this statement into pony syntax(the field time is unixtimestamp)? select DAY(FROM_UNIXTIME(time)),count(*) from userlog group by DAY(FROM_UNIXTIME(time));

this is how I did: logs = select( (datetime.fromtimestamp(l.time).day, count()) for l in UserLog if datetime.fromtimestamp(l.time).month == month and l.user == user_id)

and this is what I got: TypeError: Function 'fromtimestamp' cannot be used this way: datetime.fromtimestamp(l.time)

kozlovsky commented 6 years ago

Pony doesn't support datetime.fromtimestamp translation yet. You can use raw SQL fragment instead:

logs = select(
    (raw_sql("DAY(FROM_UNIXTIME(l.time))"), count())
    for l in UserLog
    if raw_sql("MONTH(FROM_UNIXTIME(l.time))") == month and l.user == user_id
)