【问题标题】:Create entity with no relations创建没有关系的实体
【发布时间】:2021-11-03 23:20:34
【问题描述】:

使用 SeaOrm,我想创建一个没有关系的模型。本质上是一个只有一张表的数据库。

这看起来应该超级简单,但文档没有涵盖这一点,DeriveEntityModel 宏需要所有样板文件才能存在实体关系。

我想要的是:

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

我得到的错误是:

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`

我认为必须使用另一个宏,一个不需要关系的宏,但我在文档中找不到它。

【问题讨论】:

标签: rust orm sea-orm


【解决方案1】:

感谢您试用 SeaORM。尝试定义一个空的关系枚举,如下所示。

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

【讨论】:

  • 我可以发誓我试过了...它有效,谢谢!
  • 哈哈哈哈哈。如果您有任何其他问题,请通过 GitHubDiscord 联系我们!
  • 这确实是模型生成器为我创建的。
  • Discord 链接已过期。
  • 试试这个不和谐的链接discord.com/invite/uCPdDXzbdv
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多