Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create entity with no relations

Tags:

orm

rust

sea-orm

Using SeaOrm, I want to create a model that has no relations. Essentially, a DB with one table.

This seems like it should be super easy, but the documentation doesn't cover this and the DeriveEntityModel macro requires all the boilerplate for entity relations to be present.

What I want is:

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "device")]
pub struct Model {

    #[sea_orm(primary_key)]
    pub id: i32,

    #[sea_orm(column_name = "uuid")]
    pub uuid: Uuid,

    #[sea_orm(column_name = "location")]
    pub location: Option<String>,

    #[sea_orm(column_name = "lastHeard")]
    pub lastHeard: Option<DateTime>
}

And the error I get is:

cannot find type `Relation` in this scope

help: you might have meant to use the associated type: `Self::Relation`rustc(E0412)
the trait bound `models::device::ActiveModel: sea_orm::ActiveModelBehavior` is not satisfied

the trait `sea_orm::ActiveModelBehavior` is not implemented for `models::device::ActiveModel`

I'm thinking there must be another macro to use, one that doesn't require relations, but I can't find it in the docs.

like image 977
Matthew Goulart Avatar asked Oct 17 '25 03:10

Matthew Goulart


1 Answers

thanks for trying SeaORM. Try to define an empty Relation enum like below.

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "device")]
pub struct Model {

    #[sea_orm(primary_key)]
    pub id: i32,

    #[sea_orm(column_name = "uuid")]
    pub uuid: Uuid,

    #[sea_orm(column_name = "location")]
    pub location: Option<String>,

    #[sea_orm(column_name = "lastHeard")]
    pub lastHeard: Option<DateTime>
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
like image 65
Billy Chan Avatar answered Oct 18 '25 16:10

Billy Chan