SeaQL/sea-orm · error

Reference column is not set

Error message

Reference column is not set

What it means

In `src/entity/relation.rs:472`, converting a `RelationBuilder` into a public `RelationDef` calls `.expect("Reference column is not set")` on `from_col`. The relation was constructed without ever specifying the column on the referenced (target) table. SeaORM needs both ends of a relation to build joins and foreign keys, so an unset reference column is a definition error surfaced as a panic.

Source

Thrown at src/entity/relation.rs:472

    /// Combine the column equality and [`on_condition`](Self::on_condition)
    /// with `AND` (`ConditionType::All`, default) or `OR` (`ConditionType::Any`).
    pub fn condition_type(mut self, condition_type: ConditionType) -> Self {
        self.condition_type = condition_type;
        self
    }
}

impl<E, R> From<RelationBuilder<E, R>> for RelationDef
where
    E: EntityTrait,
    R: EntityTrait,
{
    fn from(b: RelationBuilder<E, R>) -> Self {
        RelationDef {
            rel_type: b.rel_type,
            from_tbl: b.from_tbl,
            to_tbl: b.to_tbl,
            from_col: b.from_col.expect("Reference column is not set"),
            to_col: b.to_col.expect("Owner column is not set"),
            is_owner: b.is_owner,
            skip_fk: b.skip_fk,
            on_delete: b.on_delete,
            on_update: b.on_update,
            on_condition: b.on_condition,
            fk_name: b.fk_name,
            condition_type: b.condition_type,
        }
    }
}

macro_rules! set_foreign_key_stmt {
    ( $relation: ident, $foreign_key: ident ) => {
        let from_cols: Vec<String> = $relation
            .from_col
            .into_iter()
            .map(|col| {

View on GitHub (pinned to e29bcd1b41)

Solutions

  1. Add the missing column mapping: `#[sea_orm(belongs_to, from = "user_id", to = "id")]` on the relation field.
  2. For builder-based definitions, chain `.from_col(...)` and `.to_col(...)` before converting to `RelationDef`.
  3. Regenerate entities with `sea-orm-io generate entity` so relations are fully specified.

Example fix

// before
#[sea_orm(belongs_to)]
pub user: BelongsTo<super::user::Entity>,
// after
#[sea_orm(belongs_to, from = "user_id", to = "id")]
pub user: BelongsTo<super::user::Entity>,
Defensive patterns

Strategy: validation

Validate before calling

// Compile-time: ensure the relation attribute carries from/to
// Runtime smoke test at startup:
let def = RelationDef::from(<MyEntity as Related<OtherEntity>>::to_relation());
assert!(!format!("{:?}", def.from_col).contains("None"));

Type guard

fn relation_fully_specified<E: EntityTrait, R: EntityTrait>(r: &RelationDef) -> bool {
    !matches!(r.from_tbl, TableRef::default()) // and from_col/to_col set by successful From conversion
}

Prevention

When it happens

Trigger: Defining a `belongs_to` / `has_many` relation in an entity without the required `#[sea_orm(belongs_to, from = "...", to = "...")]` column mapping (or the 1.0-style `.from_col(...)`/`.to_col(...)` builder calls), then using that relation (join, load, link).

Common situations: Migrating from SeaORM 1.0 relation enums to the 2.0 `#[sea_orm::model]` attributes and forgetting the `from`/`to` mapping; typos in relation attribute keys; relations declared for views/aliased tables without explicit columns.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of SeaQL/sea-orm@e29bcd1b41 (2026-09-10). Data as JSON: /api/errors/358d0f155f36a735. Report an issue: GitHub.