Closed nico-mayora closed 10 months ago
Sorry I missed this - this is an enormously common problem when using SQLx, and one I trip up in every new project!
The problems comes in your step 2: Set up the sqlite3 database. I did not show this step in the video, and so it's as much my fault as SQLx's documentation miss, on this point.
The core conflict is: Rust has no nulls. SQL fields are nullable by default.
So, to create a SQL table that works with the following
struct Todo {
id: i64,
description: String,
done: bool,
}
Your create table statement must look something like this (taken from the SQLx example project, which I believe is correct):
CREATE TABLE IF NOT EXISTS todos
(
id INTEGER PRIMARY KEY NOT NULL,
description TEXT NOT NULL,
done BOOLEAN NOT NULL DEFAULT 0
);
If you forget to make your table columns NOT NULL
, SQLx will quite rightly wrap them with Option
, as they could be NULL
, and you'll get expected i64, found Option<i64>
, when query_as!
-ing them!
Apologies! I should have invited the viewer to check out the SQLx example project that I was demoing, I'll add that to the ERRATA and description of the video. Thank you!
https://github.com/launchbadge/sqlx/tree/main/examples/sqlite/todos
If in the future you'd like to follow along with my projects, do clone down this repository and follow the instructions in the Justfile to build each video's rust code into a full Cargo project https://github.com/0atman/noboilerplate/blob/main/scripts/justfile All my rust code from about episode 5 onwards is compiled in this way as I work on the video, so it should at the VERY LEAST compile :-)
Thanks for the response Tris, very helpful as always!
Description of the problem
I tried to get something going using The Stack, and tried using https://github.com/0atman/noboilerplate/blob/main/scripts/30-poem.md as a base. When running
cargo run
the programme won't even compile.Steps to reproduce
Cargo.toml
.main.rs
.cargo run
on the terminalExpected behaviour
The programme runs and an API is served on 127.0.0.1:3000.
Actual behaviour
Rustc fails to compile the programme, returning the following errors: