Can you use an array to create references in an Ecto Schema?

Hi, i have problem with references, i have 2 schemas:

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

	...

    field :params, {:array, :integer} #reference to categories_params.id ???
  end
  schema "categories_params" do
    field :name, :string
    field :values, {:array, :integer}

	...
  end

How (it is possible?) i can create reference with array?

Hi @dev,

Checklist of things:

1). Have you added references in your migration?
2). If yes, then you need to use Ecto’s associations

A helpful topic from Ecto’s official doc:

https://hexdocs.pm/ecto_sql/Ecto.Migration.html#references/2
https://hexdocs.pm/ecto/Ecto.Schema.html

Thanks.

Hi, thx for replay

  1. No, because this field (params) contain array of integer

It is possible create any reference to categories_params? with array?

or i just need use
has_many :categories_params, CategoriesParams
?

Hi @dev,

You can’t create references to arrays in your migrations instead you can use has_many :through or you may need relationship tables.

https://hexdocs.pm/ecto/Ecto.Schema.html#has_many/3

Thanks.

2 Likes