diesel-rs/diesel · error

expected valid identifier, found

Error message

expected valid identifier, found `{0}`. Diesel does not support column names with whitespaces yet

What it means

Diesel's field-name-to-identifier conversion tries to parse the field name as a raw Rust identifier (`r#<name>`); when that fails and the name contains a space, it reports that the name is not a valid identifier because Diesel does not yet support column names with whitespace.

Solutions

  1. Rename the database column to remove whitespace (e.g. `my_column`)
  2. Use a Rust-safe field name in your struct and map it explicitly via the supported column-name mechanism
  3. Handle the column outside diesel derives (raw SQL queries) if the schema cannot change

Example fix

// before
#[sql_name = "my column"]
my_column: String,
// after
#[sql_name = "my_column"]
my_column: String,
// or rename the DB column to `my_column`
Defensive patterns

Strategy: validation

Validate before calling

fn diesel_safe_column_name(name: &str) -> bool {
    !name.is_empty() && !name.contains(char::is_whitespace) && syn::parse_str::<syn::Ident>(&format!("r#{}", name)).is_ok()
}

Prevention

When it happens

Trigger: Deriving diesel traits where a `#[sql_name = "my column"]`/column name contains a space, causing `to_ident` to attempt `r#my column` which is not a valid Rust identifier.

Common situations: Mapping to legacy database schemas with column names containing spaces, quoting, or special characters that Rust identifiers cannot represent.

Related errors


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

Appendix: source

Thrown at diesel_attribute_parser/src/lib.rs:99

#[derive(Clone)]
pub struct SqlIdentifier {
    field_name: String,
    span: Span,
}

impl SqlIdentifier {
    pub fn span(&self) -> Span {
        self.span
    }

    pub fn to_ident(&self) -> Result<Ident> {
        match syn::parse_str::<Ident>(&format!("r#{}", self.field_name)) {
            Ok(mut ident) => {
                ident.set_span(self.span);
                Ok(ident)
            }
            Err(_e) if self.field_name.contains(' ') => Err(syn::Error::new(
                self.span(),
                format!(
                    "expected valid identifier, found `{0}`. \
                 Diesel does not support column names with whitespaces yet",
                    self.field_name
                ),
            )),
            Err(_e) => Err(syn::Error::new(
                self.span(),
                format!(
                    "expected valid identifier, found `{0}`. \
                 Diesel automatically renames invalid identifiers, \
                 perhaps you meant to write `{0}_`?",
                    self.field_name
                ),
            )),
        }
    }

View on GitHub (pinned to 6fa6ed01b2)