Model not updating, though db column udpated

i altered user table with new field country through migrations. migrate command successful and i can see the column in postgresql. but when call api its not returning country in json… and i see models/user.ex isnt reflected with new schema? any help?

Hello and welcome.

Which version of Phoenix? it seems an old one if You speak of models, now it’s schemas…

Anyway, You still need to update user.ex to reflect change of migration. In particular the schema, and changeset.

Can You show this user.ex?

1 Like

@kokolegorille How to update user.ex to reflect change of migration?
I did mix ecto.gen.migration update_users_table_with_username
then mix ecto.migrate

So what do I do to have my username added to schema and changelog?

Hello and welcome,

Can You show the migration and the User schema?

Usually the schema is quite close to the migration. You often just need to replace add by field…

1 Like

In general, you’d have something like this I think:

# migration
# you may have the `up`s and `down`s together in a def change do... alter table... add... end.
defmodule YourApp.Repo.Migrations.UpdateUsersTableWithUsername do
  ...

  def up do
    alter table(:users) do
      add :username, :string, null: false
    end
  end

  def down do
    alter table(:users) do
      remove :username, :string, null: false
    end
  end
end

And your schema generally…

defmodule do YourApp.Accounts.User do
  ...
  schema "users" do
    ...
    field :username, :string
    ...
  end

  # This may be called something else in your app.
  def registration_changeset(user, attrs, opts \\ []) do
    user
    |> cast(attrs, [..., :username, ...]
    ...
  end
end

That should be the gist of it in general… as @kokolegorille mentions the schema closely reflects the migration:

:slight_smile: :heart:

3 Likes

This was timely and helpful.