Skip to content

Computed columns #1431

@mara-schulke

Description

@mara-schulke

Motivation

Ive integrated sea orm in a quite complex project where the need arises for computed columns from different tables.

Proposed Solutions

A potential solution could be to have a computed column annotated on the model (which is not included in the derived active model) and just link a function which gets called on every selection of the model:

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, DeriveEntityModel)]
#[sea_orm(table_name = "invoice_line_item")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,

    #[sea_orm(foreign_key)]
    pub invoice_id: i64,
     
    // ..  
  
    pub amount: i64,

    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, DeriveEntityModel)]
#[sea_orm(table_name = "invoice")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,

    #[sea_orm(compute = "Entity::total")]
    pub total: i64,

    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

impl Entity {
    async fn total(pk: i64) -> Result<i64, sea_orm::error::DbErr> {
        // Run query which sums all related invoice line items
    }
}

Additional Information

I think this would be pretty useful to a lot of realworld applications, what do you think about this? 🙂

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions