diesel-rs/diesel · error · syn::Error

expected a single table name attribute note: remove this…

Error message

expected a single table name attribute
note: remove this attribute

What it means

For single-table derives, the item may carry only one `#[diesel(table_name = ...)]` attribute. When `allow_multiple_table` is false and a second `table_name` attribute is parsed, model.rs raises this error, telling you to delete the extra attribute.

Solutions

  1. Delete the duplicate `table_name` attribute, keeping exactly one.
  2. If you really need several tables, check whether your diesel version offers a multi-table/join mechanism instead of stacking attributes.
  3. Verify no attribute is duplicated across `#[diesel(...)]` groups on the same item.

Example fix

// before
#[derive(Queryable)]
#[diesel(table_name = users)]
#[diesel(table_name = posts)]
struct Row { id: i32 }

// after
#[derive(Queryable)]
#[diesel(table_name = users)]
struct Row { id: i32 }
Defensive patterns

Strategy: validation

Validate before calling

// Exactly one table_name attribute per single-table derive:
// #[diesel(table_name = users)]  // keep this
// #[diesel(table_name = posts)]  // delete this

Prevention

When it happens

Trigger: Two `#[diesel(table_name = ...)]` attributes on one struct derived with a single-table derive (Queryable/Insertable/Identifiable etc.).

Common situations: Merging code from two structs without deduplicating attributes; switching to `#[diesel(multi_table_name ...)]` semantics by just adding another table_name; copy-paste leftovers.

Related errors


AI-assisted analysis of diesel-rs/diesel@6fa6ed01b2 (2026-09-07). Data as JSON: /api/errors/890ba752dd41a83b. Report an issue: GitHub.

Appendix: source

Thrown at diesel_derives/src/model.rs:92

        let mut sql_types = vec![];
        let mut aggregate = false;
        let mut not_sized = false;
        let mut enum_type = false;
        let mut foreign_derive = false;
        let mut mysql_type = None;
        let mut mariadb_type = None;
        let mut sqlite_type = None;
        let mut postgres_type = None;
        let mut check_for_backend = None;
        let mut base_query = None;
        let mut base_query_type = None;

        for attr in parse_attributes(attrs)? {
            match attr.item {
                StructAttr::SqlType(_, value) => sql_types.push(Type::Path(value)),
                StructAttr::TableName(ident, value) => {
                    if !allow_multiple_table && !table_names.is_empty() {
                        return Err(syn::Error::new(
                            ident.span(),
                            "expected a single table name attribute\n\
                             note: remove this attribute",
                        ));
                    }
                    table_names.push(value)
                }
                StructAttr::PrimaryKey(_, keys) => {
                    primary_key_names = keys.into_iter().collect();
                }
                StructAttr::TreatNoneAsDefaultValue(_, val) => {
                    treat_none_as_default_value = Some(val)
                }
                StructAttr::TreatNoneAsNull(_, val) => treat_none_as_null = Some(val),
                StructAttr::BelongsTo(_, val) => belongs_to.push(val),
                StructAttr::Aggregate(_) => aggregate = true,
                StructAttr::NotSized(_) => not_sized = true,
                StructAttr::ForeignDerive(_) => foreign_derive = true,

View on GitHub (pinned to 6fa6ed01b2)