WilliamRen / django-pyodbc

Automatically exported from code.google.com/p/django-pyodbc
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

The date_trunc_sql function only supports year, month and day (includes solution) #112

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.  The Django Sentry app has the following code:

engine = get_db_engine(db)
        if engine.startswith('oracle'):
            method = conn.ops.date_trunc_sql('hh24', 'date')
        else:
            method = conn.ops.date_trunc_sql('hour', 'date')

        hours = max_days*24
        today = datetime.datetime.now().replace(microsecond=0, second=0, minute=0)
        min_date = today - datetime.timedelta(hours=hours)

        chart_qs = list(group.messagecountbyminute_set.all()\
                          .filter(date__gte=min_date)\
                          .extra(select={'grouper': method}).values('grouper')\
                          .annotate(num=Sum('times_seen')).values_list('grouper', 'num')\
                          .order_by('grouper'))

2. method will be returned as a null which results in:

[42S22] [Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 
'None'.

The attached operations.py solves the issue and simplifies the date_trunc_sql 
function greatly. Should work fine down to minute level.

Original issue reported on code.google.com by jacques....@gmail.com on 6 Sep 2011 at 7:20

Attachments: