Diesel: infer_schema! fails when field is `type` and maybe other reserved Rust keywords

Created on 24 May 2016  ·  3Comments  ·  Source: diesel-rs/diesel

Hello.

When compiling with diesel, if a table has a field named type, the build will fail with the following error:

error: expected identifier, found keyword `type`
 --> <diesel macros>:5:52
5 |> table_body ! { $ name ( $ pk ) { $ ( $ column_name -> $ Type , ) + } } } ;
  |>

It's possible that this problem also exists for other Rust keywords.

A reasonable solution could be suffixing the Rust field with _ (i.e. type_) for reserved keywords.

Most helpful comment

Yeah, for now there's no way to have columns with the same name as keywords in Rust. I'm planning on having some renaming convention in the future, but haven't decided on what it should be. The underscore proposal sounds reasonable.

All 3 comments

Yeah, for now there's no way to have columns with the same name as keywords in Rust. I'm planning on having some renaming convention in the future, but haven't decided on what it should be. The underscore proposal sounds reasonable.

I think having a renaming convention would be a good idea for the infer_schema macro, but I think it would be nice to be able to explicitly specify the renaming/mapping using the table macro (something like column_name="type".

Also, note that the column_name field annotation for models also doesn't allow Rust keywords.

Fixed by #1084 and #1110

Was this page helpful?
0 / 5 - 0 ratings