How are the Ecto.Schema table fields declared in the gen context mix task?

Well hello there, its my first post. Loving Elixir/Phoenix so far.

I have a question about phx.gen.context. Trying to understand how the Ecto.Schema table fields are declared in the gen context mix task. I roughly get it goes fieldname:type:options etc but wondering specifics. The :references option is confusing me as it does not say if its using belongs_to(), has_many(), or has_one() functions.

I am following through the awesome Contexts guide.

mix phx.gen.context ShoppingCart CartItem cart_items
cart_id:references:carts product_id:references:products
price_when_carted:decimal quantity:integer

Hello and welcome,

You don’t need a reference to the other table when using has_many, or has_one.

It’s the belongs_to that need a reference.

So in your case, a cart has many cart_items, while a cart_item belongs_to a cart.

Thanks for the response. After a bit more digging I found what I was looking for under Phx.Gen.Schema.

“The generator also supports references, which we will properly associate the given column to the primary key column of the referenced table”

mix phx.gen.schema Blog.Post blog_posts title user_id:references:users

I was confusing Ecto.Schemas with the phoenix schema generator syntax.