SeaQL / SeaQL/sea-orm

sea_orm struggles to support multiple sets of relations between the same two tables

Open
#3,081 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

2.0
Dominant language
Rust
Stars
9.9k
Forks
735
Avg merge
6h 36m
Merged PRs (30d)
8

Description

apologies if this is not a bug, im new to sea_orm and one of the staff in the Discord told me this was

Description

I have a database model where two tables, Owners, and MacroGroups, are related in two different ways. there is a one:many with owners owning the macrogroups, and a many:many with owners subscribing to the macro groups, via a junction table (Subscriptions). so if i try to define both of these HasMany on Owner to the same object, sea_orm cannot distinguish them, so i'd have to use manual joins and can't use the nice seaorm find_linked

Image

Steps to Reproduce

I have this code

owner.rs

//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0

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

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "owner")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,

    #[sea_orm(column_type = "Text")]
    pub name: String,

    pub is_server: bool,

    #[sea_orm(has_many)]
    pub created_macros: HasMany<super::melmacro::Entity>,

   // BUG OCCURS WITH THIS LINE
    #[sea_orm(has_many)]
    pub created_groups: HasMany<super::macro_group::Entity>,

    #[sea_orm(has_many, via = "subscription")]
    pub subscriptions: HasMany<super::macro_group::Entity>,
}

impl ActiveModelBehavior for ActiveModel {}

macro_group.rs

//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0

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

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "macro_group")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,

    pub owner_id: i64,

    #[sea_orm(
        belongs_to,
        from = "owner_id",
        to = "id",
        on_update = "NoAction",
        on_delete = "Cascade"
    )]
    pub owner: HasOne<super::owner::Entity>,

    #[sea_orm(column_type = "Text")]
    pub name: String,

    pub is_subscribable: bool,

    #[sea_orm(has_many)]
    pub macros: HasMany<super::melmacro::Entity>,

    #[sea_orm(has_many, via = "subscription", relation_enum = "Subscriber")]
    pub subscribers: HasMany<super::owner::Entity>,
}

impl ActiveModelBehavior for ActiveModel {}

subscriptions.rs

//! `SeaORM` Entity, @generated by sea-orm-codegen 2.0

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

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "subscription")]
pub struct Model {
    #[sea_orm(primary_key, auto_increment = false)]
    pub subscriber_id: i64,
    
    #[sea_orm(primary_key, auto_increment = false)]
    pub group_id: i64,
    
    #[sea_orm(
        belongs_to,
        from = "group_id",
        to = "id",
        on_update = "NoAction",
        on_delete = "Cascade"
    )]
    pub macro_group: HasOne<super::macro_group::Entity>,
    
    #[sea_orm(
        belongs_to,
        from = "subscriber_id",
        to = "id",
        on_update = "NoAction",
        on_delete = "Cascade"
    )]
    pub owner: HasOne<super::owner::Entity>,
    
}

impl ActiveModelBehavior for ActiveModel {}
Expected Behavior

i'm not an expert with sea-orm, but i want some way to, somehow, use find_related to distinguish between these relations and load one or the other

Actual Behavior

does not compile in this form.


error[E0428]: the name `MacroGroup` is defined multiple times
 --> src\db\entity\owner.rs:5:1
  |
5 | #[sea_orm::model]
  | ^^^^^^^^^^^^^^^^^ `MacroGroup` redefined here
  |
  = note: `MacroGroup` must be defined only once in the type namespace of this enum
  = note: this error originates in the derive macro `DeriveModelEx` (in Nightly builds, run with -Z macro-backtrace for more info)




error[E0277]: the trait bound `macro_group::Entity: sea_orm::Related<owner::Entity>` is not satisfied
  --> src\db\entity\owner.rs:5:1
   |
 5 | #[sea_orm::model]
   | ^^^^^^^^^^^^^^^^^ the trait `sea_orm::Related<owner::Entity>` is not implemented for `macro_group::Entity`
   |
   = help: the trait `Related<owner::Entity>` is not implemented for `macro_group::Entity`
           but trait `Related<melmacro::Entity>` is implemented for it
   = help: for that trait implementation, expected `melmacro::Entity`, found `owner::Entity`
note: required by a bound in `has_many`
  --> C:\Users\minec\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\sea-orm-2.0.0-rc.37\src\entity\base_entity.rs:95:26
   |
93 |     fn has_many<R>(_: R) -> RelationBuilder<Self, R>
   |        -------- required by a bound in this associated function
94 |     where
95 |         R: EntityTrait + Related<Self>,
   |                          ^^^^^^^^^^^^^ required by this bound in `EntityTrait::has_many`
   = note: this error originates in the derive macro `DeriveRelation` (in Nightly builds, run with -Z macro-backtrace for more info)




error[E0004]: non-exhaustive patterns: `&owner::RelatedEntity::MacroGroup` not covered
 --> src\db\entity\owner.rs:5:1
  |
5 | #[sea_orm::model]
  | ^^^^^^^^^^^^^^^^^ pattern `&owner::RelatedEntity::MacroGroup` not covered
  |
note: `owner::RelatedEntity` defined here
 --> src\db\entity\owner.rs:5:1
  |
5 | #[sea_orm::model]
  | ^^^^^^^^^^^^^^^^^
  | |
  | not covered
  = note: the matched value is of type `&owner::RelatedEntity`
  = note: this error originates in the derive macro `Debug` which comes from the expansion of the derive macro `DeriveModelEx` (in Nightly builds, run with -Z macro-backtrace for more info)

Reproduces How Often

always reproduces

Workarounds

if i omit one of the has_many fields from Owner, it works, but now i have to choose only one relation to worj with find_related

Versions

PS C:\Users\minec\RustroverProjects\melmacros> cargo tree | grep sea-
????????? sea-orm v2.0.0-rc.37
???   ????????? sea-orm-macros v2.0.0-rc.37 (proc-macro)
???   ???   ????????? sea-bae v0.2.1 (proc-macro)
???   ????????? sea-query v1.0.0-rc.31
???   ???   ????????? sea-query-derive v1.0.0-rc.12 (proc-macro)
???   ????????? sea-query-sqlx v0.8.0-rc.14
???   ???   ????????? sea-query v1.0.0-rc.31 (*)
???   ????????? sea-schema v0.17.0-rc.17
???   ???   ????????? sea-query v1.0.0-rc.31 (*)
???   ???   ????????? sea-query-sqlx v0.8.0-rc.14 (*)
???   ???   ????????? sea-schema-derive v0.3.0 (proc-macro)
Version	25H2
Installed on	‎2025-‎01-‎12
OS build	26200.8457
Experience	Windows Feature Experience Pack 1000.26100.304.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the relation declarations in owner.rs, macro_group.rs, and subscriptions.rs, then reproduce the reported errors with cargo check on SeaORM 2.0.0-rc.37. Inspect the code generated by #[sea_orm::model] for the two Owner-to-MacroGroup relations. Done means both relations compile and can be distinguished for related-record loading.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.