Hi,
I have a post
and meta_data
table. A post
has one meta_data
.
In my application, when I click the create post button, a post is created and saved to the database. However, the meta data is not. It is done from the frontend using a graphql query.
I have this changeset in the post schema:
def changeset(post, attrs) do
post
|> cast(attrs, [])
|> cast_assoc(:meta_data)
end
When I pass the meta data in, I have all the attributes except for the ID (as it isn’t guaranteed to exist). When I invoke this changeset with the following params:
%{"id" => 6, "meta_data" => %{"description" => "test", "keywords" => "test", "title" => "test"}}
I get this error message: you are attempting to change relation :meta_data...
. I understand why, it is because it is looking for a meta_data
. My update post method is as follows:
def update_post(attrs \\ %{}) do
post = get_post_for_update!(attrs.id)
Post.changeset(post, attrs)
|> Repo.update()
end
Is there a way in ecto, to find or create an association in an update changeset?