Skip to content

Add select_except #3064

@Huliiiiii

Description

@Huliiiiii

#3026 (comment)

I’d probably avoid #[lazy] on the normal Model.

If file is part of the model type but not actually loaded, then every access needs some hidden state like “loaded / not loaded / loading failed”. That makes the model less predictable and easy to misuse.

For this case I’d keep it explicit:

#[derive(Debug, FromQueryResult)]
struct EntryWithoutFile {
    id: i32,
    name: String,
}
let rows = Entity::find()
    .select_only()
    .column(Column::Id)
    .column(Column::Name)
    .into_model::<EntryWithoutFile>()
    .all(db)
    .await?;

Then load the blob only when needed:

let file: Option<Vec<u8>> = Entity::find_by_id(id)
    .select_only()
    .column(Column::File)
    .into_tuple()
    .one(db)
    .await?;

So imo a defer() / select_except() style API would fit SeaORM better than lazy fields on Model.

Something like this would be really nice:

Entity::find()
    .select_except([Column::File])
    .all(db)
    .await?;

For bigger files, I’d also consider moving the bytes to object storage / filesystem and keeping only metadata + path/key in DB. But if the files are small and DB storage is intentional, explicit projection + a helper method is probably the cleanest workaround for now.

Maybe you can wrap it like:

impl EntryWithoutFile {
    async fn load_file(&self, db: &DatabaseConnection) -> Result<Option<Vec<u8>>, DbErr> {
        Entity::find_by_id(self.id)
            .select_only()
            .column(Column::File)
            .into_tuple()
            .one(db)
            .await
    }
}

Not as magic as #[lazy], but much clearer and avoids accidentally fetching blobs in list queries.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions