Search code examples
rustrust-rocketrust-sqlx

How to fix "cannot find derive macro `FromRow` in this scope"?


I try to create a connection with a PostgreSQL databse using the Rocket framework.

This is my cargo.tml:

[package]
name = "TestRust"
version = "0.1.0"
edition = "2021"

[dependencies]
rand = "0.8.3"
rocket = { version = "=0.5.0-rc.3", features = ["secrets"] }
serde = { version = "1", features = ["derive"] }

[dependencies.rocket_dyn_templates]
version = "=0.1.0-rc.3"
features = ["handlebars", "tera"]

[dependencies.rocket_db_pools]
version = "=0.1.0-rc.3"
features = ["sqlx_postgres"]

[default.databases.ecom]
url="postgresql://localhost:5432/default?user=postgres&password=test"

and this is my main.rs:

#[macro_use] extern crate rocket;

use rocket::fs::{FileServer, relative};
use rocket::futures::{FutureExt, TryFutureExt};
use serde::{Deserialize, Serialize};
use rocket_db_pools::{Connection, Database};
use rocket_dyn_templates::{Template, context};
use rocket_db_pools::sqlx::{self, Row};
use sqlx::FromRow;

#[derive(Database)]
#[database("ecom")]
struct Logs(sqlx::PgPool);

#[derive(Deserialize, Serialize, FromRow)]
#[serde(crate = "rocket::serde")]
pub struct Produit {
    //#[serde(skip_deserializing, skip_serializing_if = "Option::is_none")]
    id: Option<i64>,
    name: String,
    fk_categorie: String,
}

and this is the error code when i build:

error: cannot find derive macro `FromRow` in this scope
  --> src\main.rs:14:34
   |
14 | #[derive(Deserialize, Serialize, FromRow)]
   |                                  ^^^^^^^
   |
note: `FromRow` is imported here, but it is only a trait, without a derive macro
  --> src\main.rs:9:5
   |
9  | use sqlx::FromRow;
   |     ^^^^^^^^^^^^^

I tried to change the version of serde but I got the same error. How do I fix it ?


Solution

  • There's a sqlx_macros feature for rocket_db_pools, but unfortunately it doesn't work because the macro requires the sqlx crate is present, so you need to add sqlx to Cargo.toml anyway. You can enable the macros feature on sqlx or the sqlx_macros feature on rocket_db_pools. Either will do the same thing.

    sqlx = { version = "*", default-features = false, features = ["macros"] }