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::Table)
.col(pk_auto(Users::Id))
.col(uuid(Users::Pid))
.col(string_uniq(Users::Email))
.col(string(Users::Password))
.col(string(Users::Name))
.col(string_null(Users::ResetToken))
.col(timestamp_null(Users::ResetSentAt))
.to_owned();
manager.create_table(table).await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Users::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Users {
Table,
Id,
Pid,
Email,
Name,
Password,
ResetToken,
ResetSentAt,
}Functions§
- Create an Alias.
- Create a primary key column with auto-increment feature.
- Create a UUID primary key
- Wrapping table schema creation.
- Add timestamp columns (
CreatedAtandUpdatedAt) to an existing table.