The error with DeriveRelation when creating a many to one relationship using SeaOrm and Rust

98 Views Asked by At

I have a new project in rust and honestly I just started using Rust. My structure is as follows

src/
  v3/
    /entity/session.rs
    /entity/mod.rs
    /entity/action.rs
    /config/config.rs
  main.rs

I want to make a many to one relationship, these are my fields

session.rs
use sea_orm::entity::prelude::*;
use strum_macros::{EnumIter};

#[derive(EnumIter, DeriveActiveEnum, Debug, PartialEq, Eq, Clone)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "device_type")]
pub enum DeviceType {
    #[sea_orm(string_value = "DESKTOP_WINDOWS")]
    DESKTOP_WINDOWS,
    #[sea_orm(string_value = "DESKTOP_MACOS")]
    DESKTOP_MACOS,
    #[sea_orm(string_value = "IOS")]
    IOS,
    #[sea_orm(string_value = "ANDROID")]
    ANDROID,
}
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "sessions")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: Uuid,
    pub fingerprint: String,
    pub clientId: String,
}

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

impl ActiveModelBehavior for ActiveModel {}
action.rs
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "actions")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: Uuid,
    pub target: Option<String>,
    pub duration: Option<f64>,
    pub idle_time: Option<f64>,
    pub filters: Option<Json>,
    pub r#type: ActionType,
    pub score: Option<f64>,
    pub processed: bool,
    pub sessionId: Uuid,
}

It should be mentioned that the tables are already made from another ORM, hence the confusion in some columns I tried to relation in the following way

action.rs
use sea_orm::entity::prelude::*;
use strum_macros::EnumIter;
use crate::v3::entity::session::Model as Session;
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(belongs_to = "Session", from = "Column::SessionId", to = "session::Column::Id")]
    Session,
}

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

impl ActiveModelBehavior for ActiveModel {}

But I encounter the following problem

error[E0423]: expected value, found struct `Session`
  --> src/v3/entity/action.rs:54:40
   |
54 |   #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
   |                                          ^^^^^^^^^^^^^^
   |
  ::: src/v3/entity/session.rs:18:1
   |
18 | / pub struct Model {
19 | |     #[sea_orm(primary_key)]
20 | |     pub id: Uuid,
21 | |     pub fingerprint: String,
...  |
34 | |     pub updatedAt: DateTimeUtc,
35 | | }
   | |_- `Session` defined here
   |
   = note: this error originates in the derive macro `DeriveRelation` (in Nightly builds, run with -Z macro-backtrace for more info)
help: use struct literal syntax instead
   |
54 | #[derive(Copy, Clone, Debug, EnumIter, Session { /* fields */ })]
   |                                        ~~~~~~~~~~~~~~~~~~~~~~~~
help: consider importing this unit variant instead
   |
1  + use crate::v3::entity::action::Relation::Session;

Is there any possible solution?

0

There are 0 best solutions below