dev
Hi,
I have problem with assoc, my repo:
def change do
create table(:categories) do
add :parrent_id, references(:categories), default: nil
add :name, :string, null: false
add :slug, :string, null: false
timestamps()
end
create index(:categories, [:parrent_id])
create unique_index(:categories, [:slug])
And my schema:
schema "categories" do
field :name, :string
field :slug, :string
belongs_to :categories, Category
has_many :categories, Category
timestamps()
end
I want create assoc parrent_id to id, what is wrong? i get error :categories is already set on schema
My target is create category struct, something looks like:
(id 3) Electronic →
(parrent_id 3) - Consoles
(parrent_id 3) - Phones
(id 4) T-shirts
…
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
Hi,
we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
Hi all, I’m new here and would like to hear peoples views on my Elixir/Phoenix based game-engine! :smiley:
Mutonex gamedesign was finish...
New
Do we have maintainer of absinthe-graphql/dataloader here? We have a PR that we’d like to merge to the library. Relatively small additive...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
(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
- #blog-post
- #ecto-query
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #hex











Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
xencor
The first argument to both “belongs_to” and “has_many” is the “name” argument. This is the name of the association that you are setting in your schema. In your case, you are trying to give your schema two associations, both with the name “:categories”, but this fails because you need to have unique names for each association.
Try changing your schema to something like this:
Note that the “foreign_key: :parent_id” additions in the schema are because :parent_id is what you have named the FK field in your migration. If you name the field something else in your migration, you should also change these in the schema.
Tee
first, i would like to know if you have a table parrent and whats the association between the two
dev
Thank you, works great
dev
I have one another question, about preload
Data struct in my table is the same:
In my example i want get all parrents (to up) categories from category of id 3 (accesories)
category = Repo.get(Category, 3) |> Repo.preload(:parent_category)And i get parent_category only “one level up”
Sorry about my english, if something is not correct, please tell me, i try write it different
xencor
I can understand your English just fine!
What you are dealing with now is “nested associations”, and you can accomplish preloading nested associations by doing something like this:
This preload should go 3 associations up. The way it works is: “|> Repo.preload(:parent_category)” preloads the parent category of the initial category. “|> Repo.preload(parent_category: [:parent_category])” would preload the parent category of the initial category, and preload the parent category of the first parent category. And you can keep chaining it from there.
One problem you may have is that I think this will throw an error if there aren’t actually 3 “levels up”, so you may need some kind of logic to handle that.
I can’t test this at the moment, but I think this should work (or is close to what you need). It’s somewhat hard to explain because we are working with a nested association within one schema. You can see the docs for preload here, which talk about nested associations, and maybe that will be clearer.
dev
Thank you, it works, but as you noticed this solution have one problem, i don’t know how deep is main category
create new field in Categories and put manually (:integer) how many neest assoc need to get main category
I have one idea, but i think is not so good
Maybe there is a better solution?
I found solution
I create simply extract function: