How to Alias Field Names in Ecto for Better Domain Modeling

255
clicks
How to Alias Field Names in Ecto for Better Domain Modeling

Source: elixirstreams.com

Type: Video

When working with existing databases in Elixir, using Ecto schemas can sometimes lead to working with legacy or poorly named columns. The article explains a feature of Ecto that allows developers to define a more suitable field name in the schema while mapping it to the original column name in the database. This is done using the `field` function in schemas, specifying a new name and using the `source:` option to point to the original column name. For example, a column named 'checked' can be aliased to 'completed' within the application, making the code cleaner and more expressive while Ecto handles the mapping to the actual database column transparently during queries.

© HashMerge 2024