SeaQL / sea-orm

🐚 An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
7.3k stars 513 forks source link

Paginator trait compiler error on EntityTrait #1104

Closed boan-anbo closed 2 years ago

boan-anbo commented 2 years ago

For some reason compiler (rustc 1.66.0) complains about not finding PaginatorTrait when used on EntityTrait (but works with actual entity).


use sea_orm::{Database, DbErr, EntityTrait, PaginatorTrait}; // < PaginatorTrait imported but the compiler doesn't pick it up

pub async fn query_with_generic_type<EntityTraitType: EntityTrait>() -> Result<(), DbErr> {

    let db = Database::connect("CONNECTION").await?;

    // find works with all()
    let mut query_all = EntityTraitType::find().all(&db).await?;

    // compiler complains about this line: `method not found in `sea_orm::Select<EntityTraitType>`
    let mut query = EntityTraitType::find().paginate(&db, 50);

    Ok(())
}

Reproduction Repo

https://github.com/boan-anbo/sea-orm-paginator-error.git

billy1624 commented 2 years ago

Hey @boan-anbo, welcome!! You need a few more trait bounds to make Rust compiler satisfy with it.

use sea_orm::{Database, DbErr, EntityTrait, FromQueryResult, PaginatorTrait};

pub async fn query_with_generic_type<E, M>() -> Result<(), DbErr>
where
    E: EntityTrait<Model = M>,
    M: FromQueryResult + Sized + Send + Sync,
{
    let db = Database::connect("CONNECTION").await?;

    // find works with all()
    let mut query_all = E::find().all(&db).await?;

    // compiler complains about this line: `method not found in `sea_orm::Select<E>`
    let mut query = E::find().paginate(&db, 50);

    Ok(())
}
boan-anbo commented 2 years ago

Thanks so much! It compiles now!