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.

Where clause with DateTime doesn't work

See original GitHub issue

I’m using the last version of Exposed 0.26.1 with sqlite jdbc. Where clause doesn’t seem to work properly, if some table has a DateTime column.

As an example I have a table TestDates

object TestDates : IntIdTable() {
    val time: Column<DateTime> = datetime("time").defaultExpression(CurrentDateTime())
}
class TestDate(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<TestDate>(TestDates)
    var time by TestDates.time
}

The table seems to be created right: CREATE TABLE TestDates (id INTEGER PRIMARY KEY AUTOINCREMENT, time NUMERIC DEFAULT (CURRENT_TIMESTAMP) NOT NULL)

Now I fill the table with test values:

for (i in 0..5) {
    Thread.sleep(10_000)
    transaction {
        TestDate.new {
        }
    }
}

It looks correctly in the database browser: 1 | 2020-06-22 15:02:22 2 | 2020-06-22 15:02:32 3 | 2020-06-22 15:02:42 4 | 2020-06-22 15:02:52 5 | 2020-06-22 15:03:02 6 | 2020-06-22 15:03:12

Now I’m trying to make a simple query:

val count = transaction {
    addLogger(StdOutSqlLogger)
    TestDate.find { TestDates.time.greater(DateTime.parse("2020-06-22").withHourOfDay(15).withMinuteOfHour(2).withSecondOfMinute(50)) }.count()
}

and as a result I get count == 6 from Exposed

But the query in log seems to be correct: SELECT COUNT(*) FROM TestDates WHERE TestDates.time > ‘2020-06-22 15:02:50.000000’ and if I execute it manually I get the right answer count == 3

Am I doing something wrong or is it actually a bug? Thank you in advance.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
hfazaicommented, Jul 31, 2020

I think I will use string representation via SQLITE_DATE_TIME_STRING_FORMATTER

1reaction
hfazaicommented, Jul 19, 2020

@yital9 Ah now I can reproduce it ! When I use your loop for (i in 0..5) { Thread.sleep(10_000) ... } to insert data I get count = 6. At the begining I inserted the values manually like below and I get count = 3 without any problem:

TestDate.new { time = DateTime(2020,6,22,15,2,22) }
TestDate.new { time = DateTime(2020,6,22,15,2,32) }
TestDate.new { time = DateTime(2020,6,22,15,2,42) }
TestDate.new { time = DateTime(2020,6,22,15,2,52) }
TestDate.new { time = DateTime(2020,6,22,15,3,2) }
TestDate.new { time = DateTime(2020,6,22,15,3,12) }

I’m gonna make some investigation to understand the cause of this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Datetime in where clause - sql - Stack Overflow
The code above won't work if the date format on the SQL Server is not set to US. Use the {ts} method below...
Read more >
SQL Where Clause With DateTime - MSDN - Microsoft
On the next page I have a SQL statement that will retrieve data from the table, with a WHERE Clause Where the Date(DateTime)...
Read more >
Troubleshooting Issues when Working with SQL Date and Time
Article focuses on troubleshooting some of the most common issues that you might face while working with the SQL time and date data...
Read more >
Date and time conditions causing SQL performance problems
SQL queries using date and time conditions are easy to get wrong. Don't convert date or time columns and never use strings to...
Read more >
Queries with datetime in WHERE fail in SQL Server 2016
DataServer queries that use a datetime value in the WHERE clause can fail when executed against Microsoft SQL Server 2016.
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