Diesel: The attribute `table_name` is currently unknown to the compiler and may have meaning added to it in the future

Created on 9 Jul 2018  路  4Comments  路  Source: diesel-rs/diesel

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
}

Most helpful comment

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:)

All 4 comments

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:)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jimmycuadra picture jimmycuadra  路  4Comments

kollapsderwellenfunktion picture kollapsderwellenfunktion  路  4Comments

Fuckoffee picture Fuckoffee  路  3Comments

killercup picture killercup  路  3Comments

ivan picture ivan  路  4Comments