Ecto assoc problem, belongs_to and has_many in one table & preload problem

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
…

1 Like

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.

3 Likes

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

Thank you, works great :slight_smile:

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

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.

3 Likes

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