simonw / django-sql-dashboard

Django app for building dashboards using raw SQL queries
https://django-sql-dashboard.datasette.io/
Apache License 2.0
437 stars 37 forks source link

Error: Object of type datetime is not JSON serializable #146

Closed matthew-a-dunlap closed 2 years ago

matthew-a-dunlap commented 2 years ago

Hello!

This project is great. I've run into an issue though. I was trying to run this (unfinished) query:

select m.id, m.pub_name, m.pub_id, m.contents_restricted, 
    array_agg(s.updated_at) as sub_dates
from main_manuscript m
left join main_submission s on m.id=s.manuscript_id
group by m.id, m.pub_name, m.pub_id, m.contents_restricted
order by m.id

The problem is that django-sql-dashboard errors out because it doesn't know how to serialize the s.updated_at DateTime field for working with array_agg.

As far as I could tell the only way to fix this would be to fork the codebase? Am I missing something? Thanks!

simonw commented 2 years ago

Definitely a bug!

simonw commented 2 years ago

Looks like this replicates the bug:

select ARRAY[now()]
simonw commented 2 years ago

Or this:

select TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'

This works and returns a column called timestamptz containing 2004-10-19 08:23:54+00:00.

This does not work:

select ARRAY[TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02']
simonw commented 2 years ago

This is the code causing the error: https://github.com/simonw/django-sql-dashboard/blob/ea3c2bebeb9b4d567991840f8046f18d9d0ffab8/django_sql_dashboard/utils.py#L53-L56

simonw commented 2 years ago

OK, fix is out in 1.0.2: https://github.com/simonw/django-sql-dashboard/releases/tag/1.0.2

matthew-a-dunlap commented 2 years ago

Awesome! Thanks so much :)