It keeps complaining about #[table_name = "users"] and I don't see what's wrong.
I have two tables in schema.rs
table! {
school (id) {
id -> Varchar,
location -> Varchar,
}
}
table! {
users (email) {
email -> Varchar,
password -> Varchar,
}
}
This is my user.rs everything looks fine to me
#[table_name = "users"]
#[derive(Serialize, Deserialize)]
pub struct User {
pub email: String,
pub password: String
}
It works for school.rs though
#[table_name = "schools"]
#[derive(Serialize, Deserialize)]
pub struct School {
pub id: i32,
pub location: String
}
This is a known issue in Rust that is caused by #![feature(proc_macro)]
, there is nothing Diesel can do about it.
Actually I lied, this is because you don't need any of the #[table_name]
attributes you've applied. Just delete them.
EDIT: I learned a lot from this guide. https://github.com/diesel-rs/diesel/blob/master/guide_drafts/trait_derives.md
Apologies for necro.
@sgrif Do you mind explaining why #[table_name]
was not required in this case and/or when it _should_ be used? I'm seemingly running into this error but my pattern doesn't match above exactly.
schema.rs
table! {
my_metadata_table {...}
}
metadata.rs
use schema::my_metadata_table;
#[table_name = "my_metadata_table"]
#[derive(Deserialize, Queryable, Serialize)]
pub struct Metadata { ... }
And I get same error as mentioned here.
Short version: Because non of the derives given expect a attribute named #[table_name]
. See the documentation of diesel_derives
to see which attribute is where supported.
Longer version: A struct implementing Queryable
(by the derive) is not related to a specific table by design. It just represents the result of a query with a specific type signature. It's possible to use structs implementing Queryable
for queries over multiple table or/and have multiple structs implementing Queryable
for a single table. Basically such structs are not 1:1 with tables but more 1:1 with queries (therefore the name :wink:)
Most helpful comment
Short version: Because non of the derives given expect a attribute named
#[table_name]
. See the documentation ofdiesel_derives
to see which attribute is where supported.Longer version: A struct implementing
Queryable
(by the derive) is not related to a specific table by design. It just represents the result of a query with a specific type signature. It's possible to use structs implementingQueryable
for queries over multiple table or/and have multiple structs implementingQueryable
for a single table. Basically such structs are not 1:1 with tables but more 1:1 with queries (therefore the name :wink:)