Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I alias a column in Elixir Ecto?

Tags:

So I have a legacy database schema that I am trying to normalize with the help of Elixir (Phoenix) and Ecto. The column definitions work fine, but they are horribly names (hooray for technical debt).

Is there a way to alias a column name, i.e. "meetingName" becomes "meeting_name" when displaying and managing it through the generated api? I've looked through the Ecto documentation and can't seem to find it at all.

Example,

@primary_key {:meetingId, :integer, []}
@derive {Phoenix.Param, key: :meetingId}
schema "meeting" do
  field :meetingName, :string

  timestamps()
end
like image 504
sudobangbang Avatar asked Jul 18 '16 15:07

sudobangbang


1 Answers

You can use the source option as documented here. This lets you specify the database column that this field refers to. For example:

field :meeting_name, :string, source: :meetingName
like image 67
Mohamad Avatar answered Sep 28 '22 04:09

Mohamad