JackMaarek
Hello guys,
Sorry for disturbing you again but I’m facing an issues that I cannot understand on my own.
I’m trying to create a image gallery api that handles images associated with categories and tags,
right now I’m focused on the association between the images and the categories and when I’m trying to send a image with a category id the image enitty is created but not associated with the category:
phoenix_1 | [info] POST /api/image
phoenix_1 | [debug] Processing with ApiAppWeb.ImageController.create/2
phoenix_1 | Parameters: %{"image" => %{"category_id" => 1, "description" => "This is a test image", "image" => "mylovelywife.png", "name" => "i1"}}
phoenix_1 | Pipelines: [:api]
phoenix_1 | [debug] QUERY OK db=2.9ms queue=0.1ms idle=9349.5ms
phoenix_1 | INSERT INTO "image" ("description","image","name","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5) RETURNING "id" ["This is a test image", "mylovelywife.png", "i1", ~N[2020-04-20 23:37:57], ~N[2020-04-20 23:37:57]]
phoenix_1 | [info] Sent 201 in 7ms
Here is my schema for my categories:
schema "category" do
field :name, :string
has_many :image, ApiApp.Images.Image
timestamps()
end
@doc false
def changeset(categories, attrs) do
categories
|> cast(attrs, [:name])
|> validate_required([:name])
|> unique_constraint(:name)
|> validate_length(:name, max: 60, count: :codepoints)
end
And the schema for the images :
schema "image" do
field :description, :string
field :image, :string
field :name, :string
belongs_to :category, ApiApp.Images.Categories, foreign_key: :category_id
timestamps()
end
@doc false
def changeset(image, attrs) do
image
|> cast(attrs, [:name, :description, :image])
|> validate_required([:name, :description, :image])
|> foreign_key_constraint(:category_id,
name: :image_category_id_fkey,
message: "Category not found!"
)
end
Also in my create_image method I’ve putted the changest’s cast_assoc method:
def create_image(attrs \\ %{}) do
%Image{}
|> Image.changeset(attrs)
|> cast_assoc(:category, with: &Category.changeset/2)
|> Repo.insert()
end
If anyone got an idea of what I’m doing wrong It would be great to hear !
Thanks a lot for your time and help guys.
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
The obligatory hello world thread!
Who are you and where are you from? :stuck_out_tongue:
New
I want to open this thread for you all to discuss and help those who really like Ash but are still hesitant to use it in a real project. ...
New
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using.
We’re particularly inte...
New
Is there a word for the ~> symbol used in Version strings?
Do you also just call it a Squiggle Arrow™ ?!
New
Other Trending Topics
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Latest Phoenix Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
axelson
I think you might want to change your parameters from:
%{"image" => %{"category_id" => 1, "description" => "This is a test image", "image" => "mylovelywife.png", "name" => "i1"}}to:
%{"image" => %{"category" => %{"id" => 1}, "description" => "This is a test image", "image" => "mylovelywife.png", "name" => "i1"}}cast_assocexpects the embed to have nested params.JackMaarek
Thanks for your help mate !
You were right when the params are nested it throws back an error again:
But I do not understand why as Category.changeset is defined in my schema…
nickdichev
Are you using the right module name? From what I can see in your errors it looks like that module might live somewhere like ApiApp.Image.Category (not sure on this just guessing) but you’re directly calling Category.changeset/2
You either need to use the full module name there or alias it if you want to use the shortened form.
JackMaarek
Yes you’re right I was just making an update about it the right module to use is the Categories module and not category.
But now the problem comes from the fallback controller…
Here is the image scheme updated:
Even with
"categories": {"id": 1}nickdichev
Can you share the contents of
api_app_web/controllers/image_controller.ex?JackMaarek
Yeah for sure !
here you go :
and the method create_image:
Thanks a lot for helping me mate !
JackMaarek
Oh and here is the fallbackController :
nickdichev
So, in your controller you have
create/2where you catch the happy path in thewithclause. What I think is happening here is that you are actually getting a changeset error that isn’t being handled. The reason I think this is the case is because of the fallback controller function definition error.You don’t have an
elseclause for thewithsocreate/2is just returning the{:error, changeset}ofRepo.insert()increate_image/1. But,{:error, changeset}isn’t a valid%Plug.Conn{}struct – the expected return of your controller functions!Since you’re not returning a
%Plug.Conn{}, Phoenix tries to help you out and call your defined fallback controllerApiAppWeb.FallbackController. You are likely missing the functioncall(conn, {:error, changeset})there (you might want to pattern match against the changeset against%Ecto.Changeset{}to avoid a catch all match…)So you have a couple options to debug from here. You can define the fallback controller
call/2and inspect in there, or put an inspect in your changeset pipeline to see what’s going wrong. (The error is actually in the previous error you posted, at the very end of the line)I know this is a lot so let me know if you have any questions! Also here’s the documentation on the fallback controller with good examples!
JackMaarek
First of all thanks a lot for this great explanation I think I’m getting it a little bit more.
If I want to pattern match through the changeset against
Ecto.Changeset{}the documentation doesn’t specify how to format ecto response in order to debug it..I have created a second call function :
In order to get which status code is sent back from the error but it still trying to call
Plug.Conn.Status.code/0And also getting the changeset pattern used by Ecto and the printed output was:
Thanks again for everything mate !
JackMaarek
I do not understand what happened but it seems that the association is working fine now.. I just recompile the project and it worked.
So that’s great, and thanks to help of this wonderful community that motivates the envy of persisting !
Now my second step is to see the associated images on GET /categories route !