The migration and schema:
alter table(:users) do
add :onboarded, :boolean, default: false
end
schema "users" do
* snip *
field :onboarded, :boolean
* snip *
end
I have a simple onboarding changeset:
def onboarding_email_changeset(struct, params) do
struct
|> cast(params, [:email])
|> validate_required([:email])
|> validate_format(:email, ~r/@/)
|> unique_constraint(:email)
|> MyApp.User.Onboarding.put_confirmation_code
end
And I use it to save a new User record.
def create_user_with_email(email) do
changeset = User.onboarding_email_changeset(%User{}, %{email: email})
case Repo.insert(changeset) do
{:ok, user} ->
user
_ ->
{:error, "User already exists with that email"}
end
end
However the user struct I get back doesn’t have the default value for onboarded set. It’s just nil, despite the default value being set correctly in my database.
# In postgresql:
# iex -S mix
# MyApp.Repo.all(MyApp.User)
[%MyApp.User{__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
confirmation_code: "574767", email: "a@email.com", encrypted_password: nil,
fullname: nil, id: 1, inserted_at: ~N[2017-06-30 16:51:55.251189],
onboarded: false, password: nil,
teams: #Ecto.Association.NotLoaded<association :teams is not loaded>,
updated_at: ~N[2017-06-30 16:51:55.258862]}]
# The struct in Elixir:
%MyApp.User{__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
confirmation_code: "522475", email: "sergio@email.com",
encrypted_password: nil, fullname: nil, id: 73,
inserted_at: ~N[2017-06-30 17:03:07.724691], onboarded: nil, password: nil,
teams: #Ecto.Association.NotLoaded<association :teams is not loaded>,
updated_at: ~N[2017-06-30 17:03:07.724702]}
Would appreciate any help, thank!