dev

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
…

Showing Posts 1 to 6

xencor

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:

schema "categories" do
  field :name, :string
  field :slug, :string

  belongs_to :parent_category, Category, foreign_key: :parent_id
  has_many :child_categories, Category, foreign_key: :parent_id

  timestamps()
end

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

Tee

first, i would like to know if you have a table parrent and whats the association between the two

def change do

    create table(:categories) do
      add :parrent_id, references(:parrent)
      add :name, :string, null: false
      add :slug, :string, null: false

      timestamps()
    end

in this scenario, parrent_id is a foreign_key which is used to reference to parrent table 


 schema "categories" do
    field :name, :string
    field :slug, :string

    belongs_to :parrent, Parrent
   

    timestamps()
  end
 
-categories belongs_to parrent this will mean that the association here will be ,either parrent has_many categories or has_one categories depending on your project structure
dev

dev OP

Thank you, works great :slight_smile:

dev

dev OP

I have one another question, about preload

Data struct in my table is the same:

Categories:
ID | parent_id | name | slug
1 / null / Electronic / electronic-1
2 / 1 / Phones / phones-2
3 / 2 / Accesories / accesories-3

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”

id: 3,
name: "Accesories"
slug: "accesories-3"
parrent_category: {

id: 2,
name: "Phones"
slug: "phones-2"
parent_category: #Ecto.Association.NotLoaded<association :parent_category is not loaded>, <- My problem - How i can load all assoc inside
}

Sorry about my english, if something is not correct, please tell me, i try write it different

xencor

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:

category = Repo.get(Category, 3) 
|> Repo.preload(parent_category: [parent_category: [:parent_category]])

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

dev OP

Thank you, it works, but as you noticed this solution have one problem, i don’t know how deep is main category
I have one idea, but i think is not so good :slight_smile: create new field in Categories and put manually (:integer) how many neest assoc need to get main category
Maybe there is a better solution?

I found solution
I create simply extract function:

defp nested_assoc_extract(schema, parents_categories) do

    case schema.parent_id do
      nil ->
        parents_categories
      _ ->
      parent = schema |> Repo.preload(:parent_category) |> IO.inspect
      nested_assoc_extract(parent.parent_category, Enum.concat(parents_categories, [%{id: parent.parent_category.id, name: parent.parent_category.name, slug: parent.parent_category.slug}]))
    end
  end
— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
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
peck
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
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
csmr
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
perzanko
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 Top

dominicletz
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
mudasobwa
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
KristerV
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews