Skip to main content
Version: 2.0.x

One to One

Rustacean Sticker Pack 🦀

Our stickers are made with a premium water-resistant vinyl with a unique matte finish. Stick them on your laptop, notebook, or any gadget to show off your love for Rust!

A one-to-one relation is the most basic type of database relation. Let say a Cake entity has at most one Fruit topping.

Defining the Relation

On the Cake entity, to define the relation:

  1. Add a new field fruit to the Model.
  2. Annotate it with has_one.
entity/cake.rs
#[sea_orm::model]
#[derive(DeriveEntityModel, ..)]
#[sea_orm(table_name = "cake")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(has_one)]
pub fruit: HasOne<super::fruit::Entity>,
}
It's expanded to:
entity/cake.rs
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_one = "super::fruit::Entity")]
Fruit,
}

impl Related<super::fruit::Entity> for Entity {
fn to() -> RelationDef {
Relation::Fruit.def()
}
}

Defining the Inverse Relation

On the Fruit entity, its cake_id attribute is referencing the primary key of Cake entity.

tip

The rule of thumb is, always define a belongs_to on the Entity with a foreign key xxx_id.

To define the inverse relation:

  1. Add a new field cake to the fruit Model.
  2. Annotate the relation with belongs_to.
  3. Implement the Related<cake::Entity> trait.
entity/fruit.rs
#[sea_orm::model]
#[derive(DeriveEntityModel, ..)]
#[sea_orm(table_name = "fruit")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
#[sea_orm(unique)]
pub cake_id: Option<i32>,
#[sea_orm(belongs_to, from = "cake_id", to = "id")]
pub cake: HasOne<super::cake::Entity>,
}
It's expanded to:
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::cake::Entity",
from = "Column::CakeId",
to = "super::cake::Column::Id"
)]
Cake,
}

impl Related<super::cake::Entity> for Entity {
fn to() -> RelationDef {
Relation::Cake.def()
}
}