SeaQL / SeaQL/sea-orm

Computed columns

Open
#2,902 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
9.9k
Forks
734
Avg merge
6h 36m
Merged PRs (30d)
8

Description

Discussed in https://github.com/SeaQL/sea-orm/discussions/1803

Originally posted by mara-schulke January 29, 2023

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? 🙂

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Discussion #1803 and the DeriveEntityModel examples included in the issue; no repository file or test is identified. The scope and completion criteria need maintainer agreement on how computed fields should be represented and queried.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.