Models & Schemas

Define a model, types, constraints, relations, migrations.

● ● ●
use runique::prelude::*;

model! {
    Product,
    table: "product",
    pk: id => Pk,
    {
        name:        text         [max_length: 255, required],
        slug:        text         [max_length: 255, required, unique],
        description: text,
        price:       decimal      [required],
        stock:       int          [default: 0],
        active:      bool         [default: true],
        category_id: fk(Category) [required],
    }
}
[required]               NOT NULL field
[nullable]               NULL field
[unique]                 UNIQUE constraint
[max_len(N)]             VARCHAR(N)
[default(value)]         SQL default value
[auto_now]               CURRENT_TIMESTAMP on creation
[auto_now_update]        CURRENT_TIMESTAMP on each update
[fk(table.col, cascade)] foreign key with ON DELETE CASCADE
[fk(table.col, null)]    foreign key with ON DELETE SET NULL
model! {
    Article,
    table: "article",
    pk: id => Pk,
    { title: text [required], }
}

// Enables Article::objects.all(), .filter(), .get(), etc.
impl_objects!(Entity);