Skip to main content

Module sql

Module sql 

Source
Expand description

SQLModel-style SQLite, PostgreSQL and MySQL persistence backed by SQLx and Tokio.

use rydantic::{Model, SqlModel, sqlx::sqlite::SqlitePoolOptions};
#[derive(Debug, PartialEq, Model, SqlModel)]
struct Note {
    #[sql(primary_key, auto_increment)]
    id: Option<i64>,
    #[model(min_length = 1)]
    text: String,
}
let pool = SqlitePoolOptions::new().max_connections(1)
    .connect("sqlite::memory:").await?;
Note::create_table(&pool).await?;
let stored = Note::parse_json(r#"{"text":"hello"}"#)?.insert(&pool).await?;
assert_eq!(stored.id, Some(1));
assert_eq!(Note::get(&pool, &1).await?.as_ref(), Some(&stored));

Structs§

Column
Database metadata inferred by #[derive(SqlModel)].
MigrationChange
One human-readable schema change, together with its data-loss classification.
MigrationOptions
Destructive migrations require an explicit opt-in for each invocation.
MigrationPlan
A preview generated from the live catalog and a model’s SQL metadata. Statements and snapshots cannot be mutated through the public API.
MigrationReport
A successful migration receipt. No-op runs have no new history revision.
Reference
Select
Parameterized queries whose driver is inferred from the supplied connection.

Enums§

Compare
Dialect
SQL syntax and schema conventions for each supported driver.
OnDelete
Order
SqlDefault
Constant SQL defaults are independent of Python-style model default factories.
SqlError
Model validation errors remain distinct from driver and schema errors.
SqlParameter
Owned parameters let a query choose its driver at execution time.
SqlType
Portable storage types mapped to native declarations by each SQL dialect.

Traits§

AutoKey
Zero or None requests an auto-generated i64 key.
Key
Supported primary keys, including optional generated keys.
SqlBackend
Driver adapter used by generated models and portable bound parameters.
SqlModel
One model with driver-inferred SQLite, PostgreSQL and MySQL operations.
SqlRow
Typed row decoding and binding for a specific SQLx driver. Generated once for all supported drivers by #[derive(SqlModel)].
SqlValue
Portable codecs shared by all SQL drivers and deferred query filters. Implement both conversions for custom field types.
ValidationConnection
Whether dropping an acquired handle returns its connection to a pool.