How SQLx Changes Database Work in Rust
Imagine this: you wrote a complex SQL query, compiled your Rust project, ran it and... got a Runtime Error because you misspelled a column name or forgot to pass one argument. Annoying, right? Especially in a language that prides itself on safety. That's exactly the problem SQLx solves.
What is it anyway
SQLx is not a typical ORM like Hibernate or SQLAlchemy. The developers call it an "SQL toolkit". The main feature of the project is that it validates your SQL queries right at compile time. If you made a typo in SELECT or try to stuff a string where the database expects a number, the Rust compiler simply won't let you build the binary.
The project is fully asynchronous and written in pure Rust (except for the SQLite driver, which pulls in a C library). It supports PostgreSQL, MySQL, and SQLite. There used to be MSSQL support, but it was removed in version 0.7, with a promise to rewrite the driver from scratch.
Query validation at compile time
This is probably the library's strongest side. Instead of building queries through method chains (DSL), like Diesel or SeaORM do, you write plain SQL inside a macro.
How does it work? During the build, SQLx connects to your database (the path is specified in the DATABASE_URL environment variable), asks it to validate the query, and returns type information. If the database says the query is correct, the macro generates an anonymous struct with the results.
By the way, if you don't want your CI server hitting the actual database every time during the build, SQLx has an "offline mode". You prepare the cache with query metadata once, and then the project builds without a network connection.
Asynchrony and performance
SQLx was designed from the ground up for async/await. It doesn't try to mimic a synchronous API wrapped in threads. The library can stream data: if you need to export a million rows, you can process them one by one as they arrive from the socket, without filling up all the memory at once.
Interestingly, the project is not tied to a specific runtime. You can use tokio, async-std, or actix — just enable the needed feature flag in Cargo.toml.
SQLx was designed from the ground up for async/await. It doesn't try to mimic a synchronous API wrapped in threads. The library can stream data: if you need to export a million rows, you can process them one by one as they arrive from the socket, without filling up all the memory at once.
Interestingly, the project is not tied to a specific runtime. You can use tokio, async-std, or actix — just enable the needed feature flag in Cargo.toml.
Why it's not an ORM
SQLx doesn't have familiar concepts like "models" or "relationships". You don't describe User has_many Posts in Rust code. You describe JOIN in the SQL query.
For those used to having full control over the database, this is a huge plus. You use the full power of your specific DBMS: specific data types (like JSONB in Postgres), window functions, or complex triggers. SQLx just takes care of the boring work of mapping results to Rust structs via the FromRow derive macro.
Small but useful details
The library has many nice little things that you usually have to look for in third-party plugins:
- Built-in connection pool. No need to plug in
r2d2or similar. - Migration support. There's a CLI utility and the
migrate!macro that embeds migrations right into your binary. - Support for
LISTEN/NOTIFYfor PostgreSQL out of the box. - Transaction handling, including nested transactions via savepoints.
Is it worth using
SQLx is perfect if you love SQL and want to use your database's capabilities to the fullest, without losing Rust's type safety.
However, you pay for compile-time query validation with build time. If you have hundreds of query! macros, cargo check can become noticeably slower. In such cases, developers recommend tuning the optimization level for macros in the development profile:
If you need a classic ORM with automatic query generation, you'd better look at SeaORM, which, by the way, is built on top of SQLx.
Overall, the project looks very mature. It has over 17,000 stars on GitHub and an active community. If you're starting a new Rust backend and aren't afraid to write SQL by hand — it's probably the best choice today.
Related projects