Replies: 2 comments 5 replies
-
|
Hi @pfandrade, var dueAt: Date
…
.where { $0.dueAt.lt(now) }Further, the .where { $0.dueAt.lt(#sql("datetime()")) }Another alternative is to define a Swift function for the current date (it can even use @DatabaseFunction
func datetime() -> Date {
@Dependency(\.date.now) var now
return now
}
…
// Install the function (typically done when creating the database):
db.add(function: $datetime)
…
.where { $0.dueAt.lt(#sql($datetime())) }And finally, whenever you do need to use custom representations for your columns, which you don't with @Column(as: Date.UnixTimeRepresentation.self)
var dueAt: Date
…
.where { $0.dueAt.lt(#bind(now.timeIntervalSince1970)) } |
Beta Was this translation helpful? Give feedback.
-
|
Yes, I wasn't aware SQLite doesn't actually compare Dates per se but strings. The root issue was that my custom SQL using DATETIME was not using sub seconds. Which would cause the comparison to fail. I don't actually need to do use julian/unix dates since I'm not doing any arithmetic. Having understood all this, I don't think anything should change on the library. Perhaps maybe a comment about dates using sub seconds and the default DATETIME function isn't. But I'm not sure where the best place to put it in the docs. I'm closing this. Thanks for all your help! |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a column that representes a Date stored as an ISO8601 string
I would like to be able perform a query like so:
.where { $0.dueAt.lt(now) } // now is of type DateThis isn't possible because Date doesn't conform to
QueryExpression.I could change it to
.where { $0.dueAt.lt(Date.ISO8601Representation(queryOutput: now)) }But this doesn't produce the right SQL because
nowis not surrounded in a DATETIME call.If I now surround that with a call to the global
dateTime()function:.where { $0.dueAt.lt(dateTime(Date.ISO8601Representation(queryOutput: now))) }I get two different errors:
Global function 'dateTime' requires that 'Date.ISO8601Representation' conform to 'SQLSpecificExpressible'Instance method 'lt' requires that 'SQLExpression' conform to 'QueryExpression'I can just use
#sql()and go about my day, but if feels like comparing dates should be easy to do using structured queries.Am I missing something?
Beta Was this translation helpful? Give feedback.
All reactions