我正在尝试在 SeaORM 迁移中创建多对多关系,但我面临一个问题,尽管我尝试过,但生成的相关特征不包含 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 方法?