I am trying to make an association between tables however I am getting hit with an error.
I currently have a “users” table with the following fields:
id | password | first_name | last_name | role | |
---|---|---|---|---|---|
1 | teacher@example.io | hash | Bob | White | teacher |
2 | student@example.io | hash | Joe | Brown | student |
I am trying to implement two new user types: teachers and students
As such, I want to make two different tables for both user types which will reference the main table like so:
- teachers table
id | user_id | first_name | last_name |
---|---|---|---|
1 | 1 (reference to “users”) | Bob | White |
- students table
id | user_id | first_name | last_name |
---|---|---|---|
1 | 2 (reference to “users”) | Joe | Brown |
I generated a new migration and created the “teachers” table like so:
create table("teachers") do
add :user_id, references("users")
add :first_name, :string
add :last_name, :string
end
I then defined a schema for the teachers table like this:
schema "teachers" do
field :user_id, :integer
field :first_name, :string
field :last_name, :string
belongs_to :user, User
Lastly, I added this to the user schema:
has_many :teachers, Teacher
After running mix ecto.migrate
the teachers table was created, but I am getting this compilation error:
== Compilation error in file lib/task_app/teachers/teacher.ex ==
** (ArgumentError) field/association :user_id is already set on schema
(ecto 3.7.1) lib/ecto/schema.ex:2137: Ecto.Schema.put_struct_field/3
(ecto 3.7.1) lib/ecto/schema.ex:1890: Ecto.Schema.define_field/4
(ecto 3.7.1) lib/ecto/schema.ex:1977: Ecto.Schema.__belongs_to__/4
lib/task_app/teachers/teacher.ex:11: (module)
I don’t know where I went wrong with the setup. But I would appreciate advice on how I can make this association.