Invalid ecto association missing field

Hi guys I do have problem. As the error states below from running ecto query association. The error is trying to find user_id but instead on my schema its employer_id, how do I link it to get all association data?

Query:

    projects =
      Ecto.assoc(user, :projects)
      |> Repo.all()

Error:

warning: invalid association `projects` in schema Ecosystem.User: associated schema Ecosystem.Project does not have field `user_id`
  lib/ecosystem/user.ex:1: Ecosystem.User (module)

User schema

  schema "users" do
    field(:email, :string)
    field(:hash_password, :string)
    field(:password, :string, virtual: true)

    has_many(:projects, Project)

    timestamps()
  end

Projects schema

  schema "projects" do
    field(:code, :string)
    field(:name, :string)
    field(:start_date, :utc_datetime)
    field(:end_date, :utc_datetime)
    field(:remarks, :string)

    belongs_to(:employer, User)

    timestamps()
  end

Your project Schema should be:

belongs_to(:user, User)

Ecto adds _id to :employer = employer_id which doesn’t exist (or I assume it doesn’t exist).

So :user = user_id and that’s what it’s expected.

Check your database table for user_id field.

Hope this helps.

Best regards.

has_many(:projects, Project, foreign_key: :employer_id)
1 Like

Hi guys, I do have a problem regarding ecto queries. As you can see below, you’ll find that I’m trying to link users table to projects table. But unfortunately, it doesn’t work as error is trying to find “users_id” column on projects table on which is named “employer_id”. How can I link it using the specified column name which is employer_id belongs_to(:employer, User)?

I’m trying to call this code:

    projects =
      Ecto.assoc(user, :projects)
      |> Repo.all()

Error:

warning: invalid association `projects` in schema Ecosystem.User: associated schema Ecosystem.Project does not have field `user_id`
  lib/ecosystem/user.ex:1: Ecosystem.User (module)

User Schema:

  schema "users" do
    field(:email, :string)
    field(:hash_password, :string)
    field(:password, :string, virtual: true)

    has_many(:projects, Project)

    timestamps()
  end

Projects Schema:

  schema "projects" do
    field(:code, :string)
    field(:name, :string)
    field(:start_date, :utc_datetime)
    field(:end_date, :utc_datetime)
    field(:remarks, :string)
    field(:status, Status, default: :pending)

    belongs_to(:employer, User)

    timestamps()
  end

Hi,
I think your schema definition doesn’t have foreign_key option set, like:

has_many(:projects, Project, foreign_key: :employer_id)

As a reference, you may read a documented options for belongs_to: https://hexdocs.pm/ecto/Ecto.Schema.html#belongs_to/3-options
and has_many:
https://hexdocs.pm/ecto/Ecto.Schema.html#has_many/3-options

If you find it difficult to resolve it, I think that the content of migration files can help resolving it.

Merged from the other thread which was a similar question about the same schemas

Thanks @lukeses, this solves my problem.