sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

DeriveColumn error

Open ringrid opened this issue 3 years ago • 3 comments

version: sea-orm 0.10.5

I tried to use custom select

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
enum QueryAs {
        CakeName,
}

but got some error for: no method named as_str found for reference &QueryAs in the current scope

ringrid avatar Dec 05 '22 03:12 ringrid

Hey @ringrid, hi there. Could you upload a reproducible example to GitHub or pastebin?

billy1624 avatar Dec 05 '22 03:12 billy1624

I found this enum had no error when together with entity, but i did not know the reason....

ringrid avatar Dec 05 '22 04:12 ringrid

entity.rs

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

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "pool")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i32,
    pub name: String,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

main.rs

use sea_orm::{
    Database, DeriveColumn, EntityTrait, EnumIter, FromQueryResult, QuerySelect,
};

mod entity;

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
enum QueryAs {
    PoolName,
}

#[derive(Debug, FromQueryResult)]
struct PoolResult {
    name: String,
}

#[tokio::main]
async fn main() {
    let db = Database::connect("xxxx").await.unwrap();

    let result1 = entity::Entity::find()
        .select_only()
        .column(entity::Column::Name)
        .into_model::<PoolResult>()
        .all(&db)
        .await
        .unwrap();

    let result2: Vec<String> = entity::Entity::find()
        .select_only()
        .column_as(entity::Column::Name, QueryAs::PoolName)
        .into_values::<_, QueryAs>()
        .all(&db)
        .await
        .unwrap();
}

DeriveColumn has error, but when I put entity.rs into main.rs,it has no error

ringrid avatar Dec 05 '22 04:12 ringrid