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
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
Hi there! :wave:
@frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
New
What IDE or editor are you using for Elixir development?
Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
New
Lately I’ve been thinking about how to organize components as a LiveView application grows. One of the pain points I’ve found (for myself...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
Other Trending Topics
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
In 2021 I started a new library called Tempo with the objective of modelling time as a set of intervals - not as instants. In 2022 I gave...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance










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 !