How to not write my data structure twice as struct & sql table?

โš“ Rust    ๐Ÿ“… 2025-08-15    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 6      

surdeus

I'm currently in the research phase for starting a new project which needs to store data persistently, and I think I'll want to use SQLite to do that in a structured, easy & fast to query way.

I'd like a setup that allows me to define the structure of my data only once - either as rust struct and generate SQL from that or the other way round - write only the create table statement and generate a rust struct from the SQL-side.

I don't want to do manual mapping of column indexes to a sensible rust struct as it looks like rusqlite would require, sqlite's api looks even less comfortable.

I'm considering to use sqlx / rbatis / ormlite - but so far I couldn't find a solution that allows me to not duplicate my data-structure-definition with one of those 3 nor any other solution.

Rbatis has a table-sync plugin which at first seemed like it could do what I wanted, but it requires the user to pass in a 3rd custom format for your data's structural definition, see also: table-sync bug: Error: E("near \"1\": syntax error") ยท Issue #592 ยท rbatis/rbatis ยท GitHub

Ormlite has a cli tool to generate migration files, but sadly it doesn't support SQLite, only Postgres. See also: sqlite: creating Model tables ยท Issue #66 ยท kurtbuilds/ormlite ยท GitHub

For sqlx I found a library that works the other way round: generating rust structs from sql tables: sql-gen - but alas, it also only supports Postgres, no SQLite so far.

Do any of you know of an existing solution for this problem?

Thank you! Kind regards, Babiro

1 post - 1 participant

Read full topic

๐Ÿท๏ธ Rust_feed