#orm #postgresql #sql


SQL data primitives. Use it to generate SQL queries, auto-generate SQL migrations, and more.

26 releases (10 breaking)

0.16.2 Jul 29, 2023
0.15.0 Jul 19, 2023
0.11.2 Mar 27, 2023

#546 in Database interfaces

Download history 25/week @ 2023-05-16 95/week @ 2023-05-23 75/week @ 2023-05-30 45/week @ 2023-06-06 35/week @ 2023-06-13 62/week @ 2023-06-20 68/week @ 2023-06-27 155/week @ 2023-07-04 199/week @ 2023-07-11 127/week @ 2023-07-18 199/week @ 2023-07-25 51/week @ 2023-08-01 71/week @ 2023-08-08 76/week @ 2023-08-15 26/week @ 2023-08-22 60/week @ 2023-08-29

237 downloads per month
Used in 12 crates (10 directly)

MIT license

1.5K SLoC

GitHub Contributors Stars Build Status Downloads Crates.io


sqlmo is a set of primitives to represent SQL tables and queries. Use these primitives to:

  • Auto-generate migrations: Load SQL representations in a standardized form (sqlmo::Schema), calculate differences between schemas (sqlmo::Migration), and generate SQL to apply the migration (sqlmo::Migration::to_sql).
  • Build SQL queries: Represent SQL queries in a data model, to create APIs for query generation. Then, generate the SQL query. Note: this library does not support parsing SQL queries (yet).

For auto-generating migrations, there are a few built-in schema sources:

If you need another source, you should define a way to build a sqlmo::Schema from your data source, then use sqlmo to auto-generate migrations.

Current tools that support this:

If you use this library, submit a PR to be added to this list.


This example reads the schema from a postgres database, defines an empty schema (which should be filled in), and then computes the migration to apply to the database.

use sqlmo_sqlx::FromPostgres;

async fn main() {
    let url = std::env::var("DATABASE_URL").unwrap();
    let mut conn = sqlx::postgres::PgConnection::connect(&url).await?;
    let current = Schema::try_from_postgres(&mut conn, schema_name).await?;
    let end_state = Schema::default(); // Load your end-state by manually defining it, or building it from another source
    let migration = current.migrate_to(end_state, &sqlmo::Options::default());
    for statement in migration.statements {
        let statement = statement.to_sql(Dialect::Postgres);
        println!("{}", statement);


  • When calculating migrations, create commented out lines for column deletion
  • ? When calculating migrations, do alter column by calculating word distance between column names