Skip to content

Add a way to persist a calculation for a resource #383

Open
@Dak425

Description

@Dak425

Is your feature request related to a problem? Please describe.
When defining generated attributes for a resource, I often see people needing to manually edit the migration to use ALTER TABLE to explicitly add the column with the SQL statement used to populate the column.

Describe the solution you'd like
A way to in an Ash resource to define an attribute that is generated, specify the raw SQL statement to populate the field, and have the generated migration use the Ecto syntax for configuring such a column shown here

Describe alternatives you've considered
Continue manually changing migrations or replace generated attribute with calculations that are not persisted in a column

Express the feature either with a change to resource syntax, or with a change to the resource interface

Idea 1 - generate_with attribute option

Using calculation

  attributes do
    attribute :generated_foo, :string, generate_with: :full_name
  end

  calculations do
    calculate :full_name, :string, expr(first_name <> " " <> last_name)
  end

Using raw SQL

  attributes do
    attribute :generated_foo, :string, generate_with: expr(fragment("? || ' ' || ?", first_name, last_name))
  end

Idea 2 - persist calculation option

  calculations do
    calculate :full_name, :string, expr(first_name <> " " <> last_name)

    persist? true
  end

Additional context
Link to the start of the conversation in the Elixir Discord server that lead to this proposal: message

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    Someday

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions