Documentation Blog Community Forum Media
|
|
Sign In
    • Ash
      • Get Started
      • Philosophy
      • Why Ash
      • Extending Resources
    • AshPostgres
      • Get Started With Postgres
    • AshSqlite
      • Get Started With Sqlite
    • AshPhoenix
      • Getting Started With Ash And Phoenix
    • AshGraphql
      • Getting Started With Graphql
    • AshJsonApi
      • Getting Started With Json Api
    • AshAuthentication
      • Auth0 Quickstart
      • Getting Started With Authentication
      • Github Quickstart
      • Magic Links Quickstart
    • AshAuthenticationPhoenix
      • Getting Started With Ash Authentication Phoenix
      • Use Ash Authentication With Liveview
    • AshStateMachine
      • Get Started With State Machines
    • AshCSV
      • Get Started With Csv
    • Reactor
      • Getting Started With Reactor
    • Spark
      • Get Started With Spark
    • Ash
      • Actions
      • Aggregates
      • Atomics
      • Attributes
      • Bulk Actions
      • Calculations
      • Code Interface
      • Constraints
      • Development Utilities
      • Embedded Resources
      • Expressions
      • Flows
      • Glossary
      • Identities
      • Managing Relationships
      • Manual Actions
      • Monitoring
      • Multitenancy
      • Notifiers
      • Pagination
      • Phoenix
      • Policies
      • Pub Sub
      • Relationships
      • Security
      • Store Context In Process
      • Testing
      • Timeouts
      • Validations
    • AshPostgres
      • Migrations And Tasks
      • Polymorphic Resources
      • Postgres Expressions
      • References
      • Schema Based Multitenancy
    • AshSqlite
      • Migrations And Tasks
      • Polymorphic Resources
      • References
      • Sqlite Expressions
    • AshPhoenix
      • Working With Phoenix
    • AshGraphql
      • Graphql Generation
      • Modifying The Resolution
      • Relay
    • AshJsonApi
      • Open Api
      • Relationships
    • AshAuthentication
      • Custom Strategy
      • Policies On Authentication Resources
      • Upgrading
    • Ash
      • Contribute
      • Define Idiomatic Actions
      • Defining Manual Relationships
      • Handle Errors
      • Structure Your Project
      • Upgrade
      • Use Without Data Layers
      • Validate Changes
    • AshPostgres
      • Join Manual Relationships
      • Test With Postgres
      • Using Fragments
    • AshSqlite
      • Join Manual Relationships
      • Test With Sqlite
      • Using Fragments
    • AshGraphql
      • Authorize With Graphql
      • Handle Errors
      • Use Enums With Graphql
      • Use Json With Graphql
      • Use Subscriptions With Graphql
      • Use Unions With Graphql
    • Spark
      • Writing Extensions
    • AshGraphql
      • Monitoring
    • AshArchival
      • Archival
      • Unarchiving
    • Ash
      • Get Started
      • Philosophy
      • Why Ash
      • Extending Resources
    • AshPostgres
      • Get Started With Postgres
    • AshSqlite
      • Get Started With Sqlite
    • AshPhoenix
      • Getting Started With Ash And Phoenix
    • AshGraphql
      • Getting Started With Graphql
    • AshJsonApi
      • Getting Started With Json Api
    • AshAuthentication
      • Auth0 Quickstart
      • Getting Started With Authentication
      • Github Quickstart
      • Magic Links Quickstart
    • AshAuthenticationPhoenix
      • Getting Started With Ash Authentication Phoenix
      • Use Ash Authentication With Liveview
    • AshStateMachine
      • Get Started With State Machines
    • AshCSV
      • Get Started With Csv
    • Reactor
      • Getting Started With Reactor
    • Spark
      • Get Started With Spark
    • Ash
      • Actions
      • Aggregates
      • Atomics
      • Attributes
      • Bulk Actions
      • Calculations
      • Code Interface
      • Constraints
      • Development Utilities
      • Embedded Resources
      • Expressions
      • Flows
      • Glossary
      • Identities
      • Managing Relationships
      • Manual Actions
      • Monitoring
      • Multitenancy
      • Notifiers
      • Pagination
      • Phoenix
      • Policies
      • Pub Sub
      • Relationships
      • Security
      • Store Context In Process
      • Testing
      • Timeouts
      • Validations
    • AshPostgres
      • Migrations And Tasks
      • Polymorphic Resources
      • Postgres Expressions
      • References
      • Schema Based Multitenancy
    • AshSqlite
      • Migrations And Tasks
      • Polymorphic Resources
      • References
      • Sqlite Expressions
    • AshPhoenix
      • Working With Phoenix
    • AshGraphql
      • Graphql Generation
      • Modifying The Resolution
      • Relay
    • AshJsonApi
      • Open Api
      • Relationships
    • AshAuthentication
      • Custom Strategy
      • Policies On Authentication Resources
      • Upgrading
    • Ash
      • Contribute
      • Define Idiomatic Actions
      • Defining Manual Relationships
      • Handle Errors
      • Structure Your Project
      • Upgrade
      • Use Without Data Layers
      • Validate Changes
    • AshPostgres
      • Join Manual Relationships
      • Test With Postgres
      • Using Fragments
    • AshSqlite
      • Join Manual Relationships
      • Test With Sqlite
      • Using Fragments
    • AshGraphql
      • Authorize With Graphql
      • Handle Errors
      • Use Enums With Graphql
      • Use Json With Graphql
      • Use Subscriptions With Graphql
      • Use Unions With Graphql
    • Spark
      • Writing Extensions
    • AshGraphql
      • Monitoring
    • AshArchival
      • Archival
      • Unarchiving
View this guide on GitHub View this guide on Hex

Attributes

Table of Contents

  1. Ways of writing attributes
  2. Special attributes
    1. create_timestamp
    2. update_timestamp
    3. uuid_primary_key
    4. integer_primary_key

Attributes specify the name, type and properties of a piece of information in a resource.

Ways of writing attributes

There are two ways to write an attribute:

attribute :name, :string, allow_nil?: false

# or ...
attribute :name, :string do
  allow_nil? false
end

Both ways will work. Though when you’re using many options the latter is preferred. This is also true of any other keyword in the Ash DSL, so you can build a flexible yet concise domain model.

For more information on attribute types including composite types and defining your own custom type see Ash.Type

You can find a comprehensive of attribute options with detailed descriptions on the d:Ash.Resource.Dsl.attributes page.

Special attributes

In Ash there are 4 special attributes these are:

  • create_timestamp
  • update_timestamp
  • integer_primary_key
  • uuid_primary_key

These are really just shorthand for an attribute with specific options set. They’re outlined below.

create_timestamp

You may recognise this if you have used Ecto before. This attribute will record the time at which each row is created, by default it uses DateTime.utc_now/1 .

create_timestamp :inserted_at is equivalent to an attribute with these options:

attribute :inserted_at, :utc_datetime_usec do
  writeable? false
  private? true
  default &DateTime.utc_now/0
  match_other_defaults? true
  allow_nil? false
end

update_timestamp

This is also similar in Ecto. This attribute records the last time a row was updated, also using DateTime.utc_now/1 by default.

update_timestamp :updated_at is equivalent to:

attribute :updated_at, :utc_datetime_usec do
  writeable? false
  private? true
  default &DateTime.utc_now/0
  updated_default &DateTime.utc_now/0
  match_other_defaults? true
  allow_nil? false
end

uuid_primary_key

This attribute is used in almost every resource. It generates a UUID every time a new record is made. uuid_primary_key :id is equivalent to:

attribute :id, :uuid do
  writeable? false
  default &Ash.UUID.generate/0
  primary_key? true
  allow_nil? false
end

integer_primary_key

Don’t use this attribute unless absolutely necessary, there are a lot of good reasons to not use autoincrementing integer ids . If you do, please make sure these resource are only accessed internally and aren’t exposed via a public API.

integer_primary_key :id is equivalent to:

attribute :id, :integer do
  writeable? false
  generated? true
  primary_key? true
  allow_nil? false
end
Source Report an issue