hi guys!
i am facing a common situation which i want to add records to my many_to_many
field , let’s say i want to add a lots of
comments to post:
schema "post" do
many_to_many(:comments, Comment, join_through: "cms_posts_comments")
# ...
end
def comment_post(post_id, body) do
with {:ok, post} <- find_content(Post, post_id),
{:ok, comment} <- create_comment(%{body: body}) do
post_with_comments = post |> Repo.preload(:comments)
post_with_comments
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_assoc(:comments, post_with_comments.comments ++ [comment])
|> Repo.update()
# die
else
{:error, reason} ->
{:error, reason}
end
end
it works , but seems the put_assoc
it`s not the right tool to do it. i programed seed 10k comments to the same post , but the server crashed in about 3300 records
def random(count \\ 10000) do
for _u <- 1..count do
CMS.comment_post(21, "fake comment")
end
end
and after about 10 mins server crashed …
[error] Postgrex.Protocol (#PID<0.261.0>) disconnected: ** (DBConnection.ConnectionError) client #PID<0.73.0> timed out because it checked out the connection for longer than 15000ms
so it’s there a better way to do it ?
how to add, update, and delete data from a many_to_many. ?