sqlx
sqlx copied to clipboard
Rust type `bool` (as SQL type `BOOLEAN`) is not compatible with SQL type `TEXT`
I am trying to fetch a column from an SQL table, the column type is boolean, but I get this error
Rust type `bool` (as SQL type `BOOLEAN`) is not compatible with SQL type `TEXT`
Here is my code
pub struct Task {
pub id: i32,
pub title: String,
pub description: String,
pub priority: Priority,
pub completed: bool,
pub due_date: String,
pub project: Project,
//reminders: Reminder[] | string,
pub created_at: String,
pub updated_at: String
}
impl Task {
pub async fn all() -> Vec<Task> {
let db = load_db().await;
let mut tasks: Vec<Task> = Vec::new();
let mut query = sqlx::query("SELECT * FROM tasks");
let mut rows = query.fetch(&db);
while let Some(row) = rows.try_next().await.unwrap() {
let task = Task {
id: row.get::<i32, &str>("id"),
title: row.get::<String, &str>("title"),
description: row.get::<String, &str>("description"),
priority: Priority::High,
completed: row.get::<bool, &str>("completed"),
due_date: row.get("due_date"),
project: Project { name: row.get("project") },
created_at: row.get("created_at"),
updated_at: row.get("updated_at")
};
tasks.push(task);
}
tasks
}
}
Sqlite Table Structure.
CREATE TABLE "tasks" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" VARCHAR(255) NOT NULL,
"description" TEXT DEFAULT NULL,
"priority" INTEGER NOT NULL DEFAULT 0,
"completed" BOOLEAN DEFAULT FALSE,
"dueDate" VARCHAR(255) DEFAULT '0000 00:00:00Z00',
"project" INTEGER NOT NULL,
"createdAt" VARCHAR(255) NOT NULL DEFAULT '0000 00:00:00Z00',
"updatedAt" VARCHAR(255) NOT NULL DEFAULT '0000 00:00:00Z00',
"reminders" TEXT DEFAULT '[]'
)
I also posted this question on stackoverflow and it can be found here. https://stackoverflow.com/questions/73229155/rust-type-bool-as-sql-type-boolean-is-not-compatible-with-sql-type-text
Why are you trying to fetch the "completed" field as a string? Do you need it as a string?
The SQL type is a Boolean, therefore it makes sense to also fetch it as a boolean..
Only then if you need it as a String, you can convert it using format!() | or | try_parse<&str>
@abonander can close as not planned