wolfiton
Hi everyone,
So the funny things is that I get no associations even though I specified the relationship in my migrations and my schema.
My associations
In create_articles migration i have add :category_id, references(:categories, on_delete: :delete_all)
In the category schema has_many(:articles, Article)
In articles schema I have belongs_to(:category, Category)
The relationship that I want to create is that an article can have only one category.But categories can belong to multiple articles.
Error trace
[info] GET /articles
[debug] Processing with BlogApiWeb.ArticleController.index/2
Parameters: %{}
Pipelines: [:browser]
[debug] QUERY ERROR source="articles" db=0.0ms queue=1.5ms
SELECT a0."id", a0."content", a0."published", a0."title", a0."category_id", a0."inserted_at", a0."updated_at" FROM "articles" AS a0 []
[info] Sent 500 in 140ms
[error] #PID<0.520.0> running BlogApiWeb.Endpoint (connection #PID<0.519.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /articles
** (exit) an exception was raised:
** (Postgrex.Error) ERROR 42703 (undefined_column) column a0.category_id does not exist
query: SELECT a0."id", a0."content", a0."published", a0."title", a0."category_id", a0."inserted_at", a0."updated_at" FROM "articles" AS a0
(ecto_sql) lib/ecto/adapters/sql.ex:629: Ecto.Adapters.SQL.raise_sql_call_error/1
(ecto_sql) lib/ecto/adapters/sql.ex:562: Ecto.Adapters.SQL.execute/5
(ecto) lib/ecto/repo/queryable.ex:177: Ecto.Repo.Queryable.execute/4
(ecto) lib/ecto/repo/queryable.ex:17: Ecto.Repo.Queryable.all/3
(blog_api) lib/blog_api/blog.ex:23: BlogApi.Blog.list_articles/0
(blog_api) lib/blog_api_web/controllers/article_controller.ex:9: BlogApiWeb.ArticleController.index/2
(blog_api) lib/blog_api_web/controllers/article_controller.ex:1: BlogApiWeb.ArticleController.action/2
(blog_api) lib/blog_api_web/controllers/article_controller.ex:1: BlogApiWeb.ArticleController.phoenix_controller_pipeline/2
(phoenix) lib/phoenix/router.ex:288: Phoenix.Router.__call__/2
(blog_api) lib/blog_api_web/endpoint.ex:1: BlogApiWeb.Endpoint.plug_builder_call/2
(blog_api) lib/plug/debugger.ex:122: BlogApiWeb.Endpoint."call (overridable 3)"/2
(blog_api) lib/blog_api_web/endpoint.ex:1: BlogApiWeb.Endpoint.call/2
(phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:42: Phoenix.Endpoint.Cowboy2Handler.init/4
(cowboy) /home/dan/Codes/blog_api/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
(cowboy) /home/dan/Codes/blog_api/deps/cowboy/src/cowboy_stream_h.erl:320: :cowboy_stream_h.execute/3
(cowboy) /home/dan/Codes/blog_api/deps/cowboy/src/cowboy_stream_h.erl:302: :cowboy_stream_h.request_process/3
(stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3
Thanks in advance
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
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
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
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
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #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)
tenzil
hey, Postgres is looking for a column category_id in articles tables and its not there, can you please post your migrations related to articles table.
I think you may you used a different column over category_id, if thats the case you need to specify the column.
wolfiton
Hi @tenzil
I am not using any custom ids.
The current config should provide a category_id to the articles table but for some strange reason it doesn’t.
I think this may actually be a bug that happens if phoenix 1.4.11 receives to many errors and also I used
mix ecto.reset.Tomorrow i will try to create a post and categories in a new project only with relationships between them, too see if this happens again.
wolfiton
My migration for articles
My article schema
My schema for categories
Kurisu
This line in
article’s changeset specifies which fields will be casted for insertion. So I assume you set the category_id later in some context function withEcto.Changeset.put_changebefore insertion. If not Postgresql will throws an error unless thecategory_idcolumn can be null.Also the problem may come from the submited form? You need to ensure user send a category
id. Please show the part of the form where users fill or select a category.Edit:
I think there are two ways to insert resource such as article that belongs to a parent (category in this case).
Ecto.Changeset.foreign_key_constraintcan be used as validator to show appropriate error message when the id sent is not valid. Also in the form if you decide to use a select, you need to format the collection of categories to be a valid enum type. For example a list like[{value_1, id_1}, {value_2, id_2}, ... ]. Values are the options that users will read in the select, and the ids are what your changeste will cast.I hope this will help you in fixing your issue.
wolfiton
Thanks @Kurisu
So here is the part where I define my assoc on article creation
I am using as guide the phoenix docs
1. Intro to Contexts — Phoenix v1.8.8 form here 1. Intro to Contexts — Phoenix v1.8.8
This part
For full code you can follow this link
https://github.com/wolfiton/blog_api
wolfiton
After I set this Ecto.Changeset.foreign_key_constraint I get a more detailed error and I think it has something to do with how i used select in my form.
Complete form code
wolfiton
I get this error when i go to the /articles/new route
Error trace
Also i don’t understand why I get this error when i am on the articles form in the create new action
Kurisu
I think you want to that when you want users to create an article and a category at the same time or update an existing category at the same time. And you need to preload an empty struct or an existing record for the association following you to create a new or update an existing one.
But given this part of your form I can assume that the category to associate to the new article already exists and just has to be selected. In this case you just have to update your article schema with an additional changeste like below:
So I just add one more field in the cast and validate_required functions:
category_idwhich will be submited by the form. Theforeign_key_constraintvalidator will throw an error if by some mean the user attempt to submit an invalid category id. So you use this changeset only to create new article unless you want to allow user to change also the category when updating an article. If you don’t want them to be able to uppdate the category of existing articles you just use your initial changeset for article update.wolfiton
Solved in this post Problem with assoc following the phoenix doc guide - #4 by wolfiton by @Kurisu
tenzil
sorry folks went out for awhile, couldn’t respond. I always do this validate the required foreign keys both in rails and phoenix.