I’m using phoenix 1.3 and I am trying populate my database (MySQL). I am new to phoenix and ecto and am trying to wrap my head around the associations.
I have 2 tables:
A(id, number, description, B_id)
B(id, number, description)
There is a 1 to many relationship between A and B. For every A there are many B.
In SQL terms I think it would look like this:
B(1, 1,“A”)
B(2, 12, “B”)
B(3, "2, “C”)
A (1, 10, “title”, 1)
A (1, 10, “title”, 2)
A (1, 10, “title”, 3)
My migration for adding the B_id is as follows:
def change do
alter table(:A) do
add :B_id, references(:A)
end
create index(:A, [:B_id])
end
In my schema has:
B -> belongs_to(A)
A -> has_many(B)
Is this correct for 1 to many relationships?