Ecto: "belongs_to_many" using postgres arrays of IDs?

Hi all,

I’m working with a many to many association where the first table, foos, has bar_ids as a postgres array of IDs, and I want to create an association such that foo belongs_to_many :bars and likewise, bar has_many :foos.

I know I can do this using a join table, but that doesn’t fit my use case. I’m also aware that I can write this as an Ecto Query, but it’s less convenient.

So, is there a way to do this as an association?

Thanks!

Nope. There is no built-in way to do that in Ecto.

But if you choose to implement with a query, you can:

  • create a virtual field fathers
  • set this field with queries
1 Like