Not sure if I remember it correctly, but when using such generator then thing which matter is order of field definition (assuming that there is no sorting in generator etc.), so for example:
defmodule MyApp.PostComment do
use Ecto.Schema
@primary_key false
schema "posts_comments" do
belongs_to :comment, MyApp.Comment, primary_key: true
belongs_to :post, MyApp.Post, primary_key: true
end
# …
end
would give: MyApp.PostComment.__schema__(:primary_key) == [:comment_id, :post_id]
and simply changing fields (here belongs_to
calls) order like:
defmodule MyApp.PostComment do
use Ecto.Schema
@primary_key false
schema "posts_comments" do
belongs_to :post, MyApp.Post, primary_key: true
belongs_to :comment, MyApp.Comment, primary_key: true
end
# …
end
would give: MyApp.PostComment.__schema__(:primary_key) == [:post_id, :comment_id]
.
Again I wrote it from memory and it could not even compile due to typo or other small mistake.
Also I’m not sure if get_by
is better in this case. In modern (of course well supported by Elixir
extension/plugin) editor and in app with no similar model names (like MyApp.Worlds
and MyApp.Words
) there should not be any problem as editor should give proper hints and auto completion. Same goes to arguments order as they would be corrected by editor app.
Finally I’m not sure if post_id
(always) should be before comment_id
. For lots of cases order really matters, for example it’s easier to write pipe calls. However in cases like here (many-to-many assoc model) we can go from comment_id
as well as from post_id
- everything depends on scenario and I don’t think that force change of order is best idea (again in this particular case).
Keep in mind that Comment
and Post
are just most popular in database examples, so even if you would always go from Post
and never from Comment
then everything change in real world app where both of sides are important unlike Comment
which is nice to have enhancement to Post
.
Of course it’s only how I think and it’s definitely not an rule from senior developer.