New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Generate entities (structs) from database #1589

Closed
flip111 opened this Issue Mar 6, 2018 · 7 comments

Comments

Projects
None yet
3 participants
@flip111

flip111 commented Mar 6, 2018

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

@Eijebong

This comment has been minimized.

Member

Eijebong commented Mar 6, 2018

diesel print-schema does exactly that

@flip111

This comment has been minimized.

flip111 commented Mar 6, 2018

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 ?

@Eijebong

This comment has been minimized.

Member

Eijebong commented Mar 6, 2018

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

@flip111

This comment has been minimized.

flip111 commented Mar 6, 2018

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

@Eijebong

This comment has been minimized.

Member

Eijebong commented Mar 6, 2018

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.

@flip111

This comment has been minimized.

flip111 commented Mar 6, 2018

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.

@dereckson

This comment has been minimized.

dereckson commented Aug 22, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment