编写sea-orm迁移中的m对m关系

问题描述 投票:0回答:1

我正在尝试在 SeaORM 迁移中创建多对多关系,但我面临一个问题,尽管我尝试过,但生成的相关特征不包含 via 方法。

背景:

  • 我已经查看了有关手动定义多对多关系的文档,但它没有涵盖特定于迁移的方面。
  • 我还尝试编写迁移代码,这导致了与手动示例类似的结构,但没有所需的 via 方法。

代码片段:

迁移

use sea_orm_migration::prelude::*;

use crate::m20240303_000002_create_caller::Caller;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Group::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Group::Id)
                            .big_integer()
                            .not_null()
                            .primary_key(),
                    )
                    .to_owned(),
            )
            .await?;

        manager
            .create_table(
                Table::create()
                    .table(GroupCaller::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(GroupCaller::Id)
                            .integer()
                            .not_null()
                            .primary_key(),
                    )
                    .col(
                        ColumnDef::new(GroupCaller::GroupId)
                            .big_integer()
                            .not_null(),
                    )
                    .col(
                        ColumnDef::new(GroupCaller::CallerId)
                            .big_integer()
                            .not_null(),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .from(GroupCaller::Table, GroupCaller::GroupId)
                            .to(Group::Table, Group::Id)
                            .on_update(ForeignKeyAction::Cascade)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .foreign_key(
                        ForeignKey::create()
                            .from(GroupCaller::Table, GroupCaller::CallerId)
                            .to(Caller::Table, Caller::Id)
                            .on_update(ForeignKeyAction::Cascade)
                            .on_delete(ForeignKeyAction::Cascade),
                    )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(Group::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
pub enum Group {
    Table,
    Id,
}

#[derive(DeriveIden)]
pub enum GroupCaller {
    Table,
    Id,
    CallerId,
    GroupId,
}

生成的代码

//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.14

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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "group_caller")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub id: i32,
    pub group_id: i64,
    pub caller_id: i64,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::caller::Entity",
        from = "Column::CallerId",
        to = "super::caller::Column::Id",
        on_update = "NoAction",
        on_delete = "NoAction"
    )]
    Caller,
    #[sea_orm(
        belongs_to = "super::group::Entity",
        from = "Column::GroupId",
        to = "super::group::Column::Id",
        on_update = "NoAction",
        on_delete = "NoAction"
    )]
    Group,
}

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

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

impl ActiveModelBehavior for ActiveModel {}

问题:

我的方法是否遗漏了什么?在 SeaORM 迁移中建立多对多关系需要哪些步骤,包括在相关特征中生成 via 方法?

附加信息:

  • SeaORM版本:0.12
  • 使用的数据库:PostgerSQL
database postgresql rust orm sea-orm
1个回答
0
投票

除了一件事之外,你做得对。 连接表应遵守以下要求,它应该具有:

  1. 正好 2 个外键
  2. 由 2 列组成的主键
  3. 主键数等于列数

参考

© www.soinside.com 2019 - 2024. All rights reserved.