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
Hey there,
It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Quite interesting article Google brought me. Didn’t find any mentions about it here.
What do you think in general? Would you use togethe...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
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
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
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
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security











Most Liked- 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.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!
axelson
If you’re trying to debug what’s happening you should use IO.inspect instead of IO.puts:
More info at:
Last Post!
JackMaarek
I see this is very helpful.
Thanks for the ressource !