Wrapping my head around 1 to many relationships in Phoenix and ecto

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?

It’s the belongs_to side that holds reference to the other table…

That would be with your schema

B has many A
A belongs_to B

also … id should be unique, that is more like

B (1, 1, “A”)

A (1, 10, “title”, 1)
A (2, 10, “title”, 1)
A (3, 10, “title”, 1)

@kokolegorille is right.

Say we have a Contact table, and each contact has an Address selected from a list of addresses. You could either describe this as an address has many contacts, or a many contacts have an address. In your mind, it is intuitive to say each contact has an address. However, Ecto is unaware of the real world context of our models and only cares about relationships.