Search code examples
rustormsea-orm

Create entity with no relations


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.


Solution

  • 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 {}