jiff: Cannot use `jiff-sqlx` with SQLx's `query!` and `query_as!` macros

I attempted to migrate one of my projects from chrono to jiff using the jiff-sqlx crate, but it appears that jiff-sqlx is not compatible with the query! and query_as! macros which are used for static type checking of SQL queries (not to be confused with the query and query_as functions).

I’ve created a minimal reproducer here: https://github.com/tonyfinn/jiff-sqlx-poc/tree/main

but the gist is that if you don’t enable any of the other time crates then a crate using sqlx fails to build as it there is no From<()> implementation for the jiff_sqlx types, and tells you you need to enable the time feature to use the TIMESTAMPTZ type (example error output) and if you do enable the other time crates then it complains that e.g. time::OffsetDateTime and jiff_sqlx::Timestamp are not the same type.

(Let me know if you want me to file this as a seperate issue, just keeping it with the existing sqlx discussion as its newly implemented)

I suspect this will require changes inside sqlx to make it work

_Originally posted by @tonyfinn in https://github.com/BurntSushi/jiff/issues/240#issuecomment-2670312085_

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Sure, you need to have the database setup first. The SQL script to do that is in the migrations directory. You can run that manually or you can use sqlx database create then sqlx migrate run with sqlx-cli (after setting DATABASE_URL to your database connection settings). After that cargo build will produce the errors in the text files.


A small bit of sqlx info (which you can ignore if you want to set up a test DB) about the cargo sqlx prepare is that it’s a tool that basically produces a JSON file storing the database schema that is used for compile time type checks in lieu of the database so you don’t need a running database to compile. I should have committed those files to save you from setting up a DB, but I forgot last night. When I finish work today I’ll generate those files and commit them to simplify reproduction. However, actually running cargo sqlx prepare needs the database up and available.

@bionicles You can ignore the nix stuff if you’re not using Nix yourself, you just need to make sure you have postgres and sqlx-cli and cargo installed beforehand then.

The .envrc stuff sets up a default postgres connection string, but you can set that manually with an export command. I don’t know what user/password/etc you use for the local postgres, so I can’t really provide you a DATABASE_URL variable for that, just that it needs to be pointed to a blank database.

I’ve updated the sample repo to include a basic readme and to fix the non-sqlx related errors. That’s my bad, I was expecting compiler errors so I didn’t check to see if any of them were surplus. It should now just be the jiff-sqlx ones.

Actually, realised I could do it from my laptop, so I’ve pushed the sqlx schema files to the repo now: https://github.com/tonyfinn/jiff-sqlx-poc/blob/main/.sqlx/query-ef8165b29a48650641e5577eda029c2717c47c95883442188f8970770d2c7580.json

You’ll still need a running database to cargo run the example, but cargo build should now produce the same errors for you as it did for me even without a local database set up.