Adding a new database column, do I need to update changeset function?

I have a table where I am adding a new column called “num_matches”. I am adding the field to the code. Do I also need to make changes to the changeset function? Note that this field is optional and is ok to be NULL.

defmodule Teiserver.Account.Rating do
  use TeiserverWeb, :schema

  @primary_key false
  schema "teiserver_account_ratings" do
    belongs_to :user, Teiserver.Account.User, primary_key: true
    belongs_to :rating_type, Teiserver.Game.RatingType, primary_key: true
    field :rating_value, :float
    field :skill, :float
    field :uncertainty, :float
    field :leaderboard_rating, :float
    field :last_updated, :utc_datetime

    #This is a new column
    field :num_matches, :integer
  end

  @doc false
  def changeset(stats, attrs \\ %{}) do
    stats
    |> cast(
      attrs,
      ~w(user_id rating_type_id rating_value skill uncertainty last_updated leaderboard_ratin)a
    )
    |> validate_required(
      ~w(user_id rating_type_id rating_value skill uncertainty last_updated leaderboard_rating)a
    )
  end
end

If you want it parsed and then stored then include it in the cast parameters. And if it is optional then do NOT include it in validate_required.

2 Likes