Reverse engineer DB in Diesel


#1

I had a little look but I couldn’t find a definitive answer.
I was just wondering if Diesel supported a way to build models.rs from an existing DB?
If I did see it, I didn’t catch it :slight_smile:


#2

I think you’re looking for the diesel print-schema command. It’ll use whatever database is pointed to by the DATABASE_URL environment variable.

They talk about it about half way down the getting started guide. You’re looking for a line containing “diesel print-schema > src/schema.rs”.


#3

If you were not looking for diesel print-schema: With model.rs, do you mean a file that contains Rust structs mapping to tables (with their fields mapping to columns)? This is not generally how you’d use diesel (IMO) – the structs don’t need to map to the tables 1:1, but instead should map to what data you get/send to the database.


#4

Thanks, I guess yes to the second part of your question. I was looking for something like SQLAlchemy in Flask or Gii in Yii2. Which scans the DB and spits outs a model that defines as best it can the types each column has. I guess I should rethink this approach if using Diesel.


#5

Ok thanks, but these need to be built manually? Are there existing guides on how this might look with a more complex multiple table example?


#6

Hi, I exactly have a code that do these. It is used in my database interface app, the underlying model is extracted from database through a query against pg_tables.
Here is the excerpt of the query