Validate required not working correctly

I have a chat app where I want to store messages that are sent by a user in the database . I am using ecto for the project. In the code below you can see that I am providing chat_id that is a required field but its still returning that its blank.

Any help would be appreciated.

Repo.insert( PersonalMessage.changeset(%PersonalMessage{}, %{user_id: "dd8c3258-6749-4fc7-8470-eeb6cffa7137", chat_id: "ee4acecb-9e2d-4429-b9d2-4b07eb671eb2", text: "fjkdf"}))                  
{:error,
 #Ecto.Changeset<
   action: :insert,
   changes: %{text: "fjkdf", user_id: "dd8c3258-6749-4fc7-8470-eeb6cffa7137"},
   errors: [chat_id: {"can't be blank", [validation: :required]}],
   data: #Chats.PersonalMessages.PersonalMessage<>,
   valid?: false
 >}

Welcome !

Can we see the content of the function PersonalMessage.changeset/2 ?
(also, you should put your code between triple back-ticks, otherwise, it makes the code harder to read).

2 Likes

I think you might’ve forgotten to cast chat_id since it’s not present in changeset.changes.

Hi. I think the changeset function was created by default. I am new to phoenix and elixir so I might be wrong

Yes. That was it. Thanks a lot!!