Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self referencing belongs_to relations #94

Closed
billy1624 opened this issue Nov 8, 2022 · 1 comment · Fixed by #99
Closed

Self referencing belongs_to relations #94

billy1624 opened this issue Nov 8, 2022 · 1 comment · Fixed by #99

Comments

@billy1624
Copy link
Member

Motivation

One could define child-to-parent relation and query parent::Model on seaography. However, the parent-to-child relation will fetch child::Model but not Vec<child::Model>.

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation, RelationsCompact)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::deck::Entity",
        from = "super::deck::Column::ParentId",
        to = "super::deck::Column::Id",
    )]
    Parent,
    #[sea_orm(
        belongs_to = "super::deck::Entity",
        from = "super::deck::Column::Id",
        to = "super::deck::Column::ParentId",
    )]
    Child,
}

Proposed Solutions

A #[seaography(has_many)] proc attribute to override the default: fetching Model instead of Vec<Model> for belongs_to relation.

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation, RelationsCompact)]
pub enum Relation {
    #[sea_orm(
        belongs_to = "super::deck::Entity",
        from = "super::deck::Column::Id",
        to = "super::deck::Column::ParentId",
    )]
    #[seaography(has_many)]
    Child,
}

I believe we need to change this part in the derive macro.

let (return_type, extra_imports, map_method) = if has_many.is_some() {
(
quote! { Vec<#path::Model> },
quote! { use seaography::itertools::Itertools; },
quote! { .into_group_map() },
)
} else if belongs_to.is_some() {
(quote! { #path::Model }, quote! {}, quote! { .collect() })
} else {
return Err(crate::error::Error::Internal(
"Cannot map relation: neither one-many or many-one".into(),
));
};

Additional Information

Context: https://discord.com/channels/873880840487206962/1039063562083127326

@karatakis
Copy link
Collaborator

I can work on this after #84 is merged

karatakis added a commit that referenced this issue Nov 14, 2022
## PR Info
- Closes #94

## New Features

- [X] Relations derive produces the reverse of a SelfRef relation

## Bug Fixes

- [X] Relations derive could not handle properly SelfRef relations
@billy1624 billy1624 moved this from Triage to Done in SeaQL Dev Tracker Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants