Using ecto changeset to handle fields

I’m trying to create a POST route to create a book in the database, but the body params are different than the Book schema fields, I tried something like :

POST body :

"book" : {
"title": "test"

In the schema, that field is called “book_title”, how can I associate them ? I tried

    book = %Book{
      book_title: attrs["title"]
    changeset = Book.changeset(book, attrs)
    case Repo.insert(changeset)  do

But now it doesn’t respect the constraints (length etc…) in my changeset

1 Like

You actually want to pass that in as attrs:

    new_attrs = %{
      book_title: attrs["title"]
    changeset = Book.changeset(%Book{}, new_attrs)
    case Repo.insert(changeset)

Now the changeset will take the empty schema, and try to put the new attrs