question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

func.Date returns str instead of date object in sqlite

See original GitHub issue

We know that sqlite does not have date or datetime functionalities, and uses strings instead. And this functionality is provided by SQLAlchemy.

It works great on simple queries like this:

dates = (db.session.query(Session.starts_at)
                 .filter_by(state='accepted')
                 .distinct().all())

This returns datetime objects - [(datetime.datetime(2019, 10, 15, 10, 25, 46),), (datetime.datetime(2019, 10, 15, 11, 30),), (datetime.datetime(2019, 10, 16, 9, 15, 30),), (datetime.datetime(2019, 10, 16, 11, 30, 30),)]

Let’s say we want to select distinct dates and we use CAST

dates = (db.session.query(cast(Session.starts_at, DATE))
                 .filter_by(state='accepted')
                 .distinct(),all())

This fails with the error:

Couldn't parse date string '2019' - value is not a string.

This is expected as it relies on sqlite CAST which returns year for some reason.

The closest I got to getting correct result(and I did get correct results), was using func.Date

dates = (db.session.query(func.date(Session.starts_at))
                 .filter_by(state='accepted')
                 .distinct().all())

This returns correct results - [('2019-10-15',), ('2019-10-16',)]

But, as we can see, they are strs Is there anyway SQLAlchemy can convert them in date objects as func.Date is SQLAlchemy internal and it already has https://docs.sqlalchemy.org/en/13/dialects/sqlite.html#sqlalchemy.dialects.sqlite.DATE which has all the logic needed for parsing this date.

So, can we get [(datetime.date(2019, 10, 15),), (datetime.date(2019, 10, 16),)] as result? Am I missing something or is there any workaround for this?

P.S. I don’t know if https://gist.github.com/zzzeek/7470863 was related to this, but it doesn’t help as well.

Thanks

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
zzzeekcommented, Oct 17, 2019

you want to call the func like this:

func.date(Session.starts_at, type_=Date)

that will tell it the return type is Date and it will do the coercion

1reaction
zzzeekcommented, Oct 17, 2019

the docs are horrible for func, there’s nothing there about how to use it at all, nor is there any real usage guidelines or parameter descriptions. people know this from the tutorial at https://docs.sqlalchemy.org/en/13/core/tutorial.html#functions but the docstrings should be pointing back there. all of them should be a little more clear that type_ is fairly important for cases like this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Date And Time Functions - SQLite
The datetime() function returns the date and time as text in their same formats: YYYY-MM-DD HH:MM:SS.
Read more >
SQLite date() Function By Practical Examples
The date() function accepts a time string and zero or more modifiers as arguments. It returns a date string in this format: YYYY-MM-DD...
Read more >
How to read datetime back from sqlite as a ... - Stack Overflow
I just find out it is the detect_types=sqlite3.PARSE_DECLTYPES parameter of the connect function that makes fetchall returns a datetime. If you ...
Read more >
SQLite Date() Function - Tutlane
The SQLite date() function takes datetimestring and one or more modifier values and returns date in a YYYY-MM-DD format. It is important to...
Read more >
How to Format a Datetime in SQLite - LearnSQL.com
Use the STRFTIME() function to format date\time\datetime data in SQLite. This function takes two arguments. The first argument is a format string containing ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found