How to name for different columns from one column

I have one table Teams which has primary key (id).
The other table refer to key (id) as different columns. home_team_id and away_team_id.

In case of RoR. I can write following code for model.

belongs_to :home_team, class_name: Team, foreign_key: 'home_team_id'
belongs_to :away_team, class_name: Team, foreign_key: 'away_team_id'

How can I write for Phoenix or Ecto way?


belongs_to :home_team, Team
belongs_to :away_team, Team

This will just straight up work. The foreign key defaults to #{association_name}_id so you don’t even need to specify it.

1 Like

When I try to preload team from game, the following error happen.

# schema Apps.Sample.Game does not have association :team

I should have written following. I figured out.

    query = from g in Game,
            preload: [:away_team, :home_team]


1 Like