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

Model with Generics #402

Closed
oscartbeaumont opened this issue Dec 25, 2021 · 3 comments · Fixed by #400
Closed

Model with Generics #402

oscartbeaumont opened this issue Dec 25, 2021 · 3 comments · Fixed by #400
Assignees

Comments

@oscartbeaumont
Copy link

I am working on a crate called async-graphql-relay and have recently redone the public API to make it less cumbersome to use (you can checkout the example folder in the repo). When doing this I introduced a type RelayNodeID which is generic on the Model so it is declared as RelayNodeID<Self>. This is done so that among other things all ID's returned from GraphQL are globally unique and contain information about the specific type they are. This type is a wrapper around a standard Uuidv4 and from a database perspective should be treated this way.

After chatting with @billy1624 he pointed me towards this resource which shows how to implement sea-orm support for custom types. The issue I found with this is that my type is generic and this does not currently seem to be supported by sea-orm. When I run my code the DeriveEntityModel macro panics (refer to the code linked below).

I put together some code here which should help show the sort of syntax I am going for without all the complexity and macros of the async-graphql-relay codebase.

Thanks, @billy1624 for creating PR #400 which is going to be the fix for this.

@tyt2y3
Copy link
Member

tyt2y3 commented Dec 25, 2021

❄️🎄

@billy1624 billy1624 self-assigned this Dec 25, 2021
@billy1624
Copy link
Member

With #400 you could define the following:

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: AccountId<String>,
    pub name: String,
}

But not...

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "model")]
pub struct Model<T> {
    #[sea_orm(primary_key)]
    pub id: AccountId<T>,
    pub name: String,
}

So, I'm think should we also support the use of generics over Model? @tyt2y3

@oscartbeaumont
Copy link
Author

PR #400 works perfectly for my usecase. Thanks, @billy1624.
Something to note was that although my original idea was RelayNodeID<Self> this causes a lot of issues because the ID field is copied to some SeaORM internal stuff (Eg. ActiveModel, etc) and those types do not satisfy the expected trait for my code. I fixed this easily by changing it to RelayNodeID<Model>. This could be worth noting in documentation although this is probably something a Rust developer would be able to work out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants