Lets take this example
create table(:barcodes, primary_key: false) do
add :id, :binary_id, primary_key: true
add :name, :string
add :barcode, :string
add :product_id, references(:products, on_delete: :nothing, type: :binary_id)
timestamps()
end
Schema
schema "barcodes" do
field :barcode, :string
field :name, :string
field :admin_id, :binary_id
field :user_id, :binary_id
has_one(:product, Logistics.Products.Product)
timestamps()
end
Query
query = from b in Barcode,
distinct: b.id,
where: b.admin_id == ^admin_id,
left_join: p in assoc(b, :product),
preload: :product
Repo.all(query)
The above one perfectly works for me.
But what if I want to add a set of product ids in a single field like array instead of single product id.
Ex:
create table(:barcodes, primary_key: false) do
add :id, :binary_id, primary_key: true
add :name, :string
add :barcode, :string
add :product_id, {:array, :string}
timestamps()
end
Schema:
field :product_id, {:array, :string }, default: []
It will be stored as
product_id: [“888fe8a8-4de9-4f14-a596-f333f8b02e6d”, “988fe8a8-3de9-4f14-a596-f333f8b02e6d”, … … …]
Now when I want to query the barcodes along with the preloaded product ids.
Any insight on how to achieve this will be greatly appreciated. Thanks