I am starting to use the Diesel ORM for an application, and am confused about the necessary steps described in the official getting started guide.
It seems like the ORM first requires to create type definitions in SQL, placed in a set of “migrations” files (up.sql
and down.sql
). The ORM then automatically generates some Rust type definitions (schema.rs
) based on those files, but also requires us to create the same type definitions again, manually, in another Rust file (models.rs
).
I am strugging to understand the intended workflow, and the reasons why a duplicate type definition is necessary. If there are two files that need to be manually written to describe the same types for the same data, in two different languages, several potential problems appear:
- one needs to always remember to update both definitions when the shape of the data changes, one of which is boilerplate code
- the potential for human error and mismatched types is introduced
- the source of truth for the data shape is unclear
- it’s necessary to correctly define exactly the same types across languages (i.e. to be careful that an
INTEGER
in SQL is always exactly ai32
in the Rust file)
Maybe I misunderstand the tutorial. Regardless, there is one main question:
Is there are a way to automatically generate all the needed Rust type definitions from the SQL definitions, or vice versa, to keep one source of truth for the data shape?