Skip to main content

Module schema

Module schema 

Source
Expand description

Adapted from https://github.com/loco-rs/loco/blob/master/src/schema.rs

§Database Table Schema Helpers

This module defines functions and helpers for creating database table schemas using the sea-orm and sea-query libraries.

§Example

The following example shows how the user migration file should be and using the schema helpers to create the Db fields.

use sea_orm_migration::{prelude::*, schema::*};

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        let table = table_auto("users")
            .col(pk_auto("id"))
            .col(uuid("pid"))
            .col(string_uniq("email"))
            .col(string("password"))
            .col(string("name"))
            .col(string_null("reset_token"))
            .col(timestamp_null("reset_sent_at"))
            .to_owned();
        manager.create_table(table).await?;
        Ok(())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table("users").to_owned())
            .await
    }
}

Functions§

array
array_null
array_uniq
big_integer
big_integer_null
big_integer_uniq
big_pk_auto
Create a primary key column of big integer with auto-increment
big_unsigned
big_unsigned_null
big_unsigned_uniq
binary
binary_len
binary_len_null
binary_len_uniq
binary_null
binary_uniq
bit
bit_null
bit_uniq
blob
blob_null
blob_uniq
boolean
boolean_null
boolean_uniq
char
char_len
char_len_null
char_len_uniq
char_null
char_uniq
custom
custom_null
date
date_null
date_time
date_time_null
date_time_uniq
date_uniq
decimal
decimal_len
decimal_len_null
decimal_len_uniq
decimal_null
decimal_uniq
double
double_null
double_uniq
enumeration
enumeration_null
enumeration_uniq
float
float_null
float_uniq
integer
integer_null
integer_uniq
interval
interval_null
interval_uniq
json
json_binary
json_binary_null
json_binary_uniq
json_null
json_uniq
money
money_len
money_len_null
money_len_uniq
money_null
money_uniq
name
Create an Alias.
pk_auto
Create a primary key column with auto-increment
pk_uuid
Create a UUID primary key
small_integer
small_integer_null
small_integer_uniq
small_unsigned
small_unsigned_null
small_unsigned_uniq
string
string_len
string_len_null
string_len_uniq
string_null
string_uniq
table_auto
Create a table with created_at and updated_at added by default
text
text_null
text_uniq
time
time_null
time_uniq
timestamp
timestamp_null
timestamp_uniq
timestamp_with_time_zone
timestamp_with_time_zone_null
timestamp_with_time_zone_uniq
timestamps
Add timestamp columns (CreatedAt and UpdatedAt) to an existing table.
tiny_integer
tiny_integer_null
tiny_integer_uniq
tiny_unsigned
tiny_unsigned_null
tiny_unsigned_uniq
unsigned
unsigned_null
unsigned_uniq
uuid
uuid_null
uuid_uniq
var_binary
var_binary_null
var_binary_uniq
varbit
varbit_null
varbit_uniq
year
year_null
year_uniq