Diesel: Feature request: Generate entities (structs) from database

Created on 6 Mar 2018  路  7Comments  路  Source: diesel-rs/diesel

I have an existing database with tables, it would be very nice if diesel could inspect the database and generate the rust code for the models. Here is an example of the process using a different language and orm https://symfony.com/doc/current/doctrine/reverse_engineering.html

Most helpful comment

If someone is looking for such feature,
see https://crates.io/crates/diesel_cli_ext. This crate provides a feature to generate model structs.

All 7 comments

diesel print-schema does exactly that

diesel print-schema does exactly that

I see the table! macro printed with information about the columns ... how do i get the models from that ?

Do you mean Queryable structs ? You don't. You have to write them yourself.

queryable and insertable ... it would be nice if this code can be generated

IMO this is out of the scope of diesel and since SQL types aren't 1 <=> 1 with rust types we couldn't really generate them anyway.
You could reuse the code from diesel_infer_schema to write such a tool though.

It's possible but with the caveat that is described in the symfony link

Doctrine is able to convert approximately 70-80% of the necessary mapping information based on fields, indexes and foreign key constraints. Doctrine can't discover inverse associations, inheritance types, entities with foreign keys as primary keys or semantical operations on associations such as cascade or lifecycle events. Some additional work on the generated entities will be necessary afterwards to design each to fit your domain model specificities.

Same for type mapping ... a best guess could be done. I think since already the schema can be printed it shouldn't be too hard to print the rust structs as well. I have no rust experience though, for me it will be hard in this point of time.

I will close this because it's considered out of scope for diesel itself.

If someone is looking for such feature,
see https://crates.io/crates/diesel_cli_ext. This crate provides a feature to generate model structs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pwoolcoc picture pwoolcoc  路  3Comments

gliderkite picture gliderkite  路  3Comments

killercup picture killercup  路  3Comments

jimmycuadra picture jimmycuadra  路  3Comments

kanekv picture kanekv  路  3Comments