erd.sketch

SQL to Rust: models from a DDL script

What follows is not a mock-up: it is the real output of the generator for the script on the left. You get the same code in the editor under “Codegen → Rust”.

The inputschema.sql
CREATE TABLE users (
    id BIGINT NOT NULL PRIMARY KEY,
    email       VARCHAR(255) NOT NULL UNIQUE,
    full_name   VARCHAR(120),
    is_active   BOOLEAN NOT NULL,
    created_at  TIMESTAMP NOT NULL
);

CREATE TABLE orders (
    id BIGINT NOT NULL PRIMARY KEY,
    user_id     BIGINT NOT NULL REFERENCES users(id),
    total       DECIMAL(12,2) NOT NULL,
    note        TEXT,
    placed_at   TIMESTAMP NOT NULL
);
What comes outmodels.rs
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Users {
    pub id: i64,
    pub email: String,
    pub full_name: Option<String>,
    pub is_active: bool,
    pub created_at: chrono::NaiveDateTime,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct Orders {
    pub id: i64,
    pub user_id: i64,  // → users.id
    pub total: rust_decimal::Decimal,
    pub note: Option<String>,
    pub placed_at: chrono::NaiveDateTime,
}
Open the editor

The script stays in the browser: both parsing and generation run on your machine, nothing goes to a server.

How the types land

in SQLin the model
BIGINTi64
VARCHAR(255)String
BOOLEANbool
TIMESTAMPchrono::NaiveDateTime
DECIMAL(12,2)rust_decimal::Decimal
TEXTOption<String>
Optional field
Option<String>
Column name
full_name
Relation
// → users.id