acrolink
Is there a built-in Ecto way to define associations by an array of integers field?
Hi,
Table A:
Dishes: id and name fields.
Table B:
Users: with a field called favorite_dishes holding an array of id’s (of dishes).
Is there an Ecto way to treat this as an association ? e.g. for pre-loading ?
How to best do that. I am using PostgreSQL.
Thank you
Most Liked
benwilson512
In general it is better to use a join table instead of an array of integers. This ensures you can use foreign key references, and will index better. There is no built in support for driving associations via an array of integers. If you’re just doing has many then the associated table should hold the parent id. If you need M:N associations, use a join table.
FlyingNoodle
We have done this at work and we are in a world of pain because of it.
Don’t do it.
benwilson512
I would definitely start with the “normal” way of modeling this in Postgres which would be via join tables, and then optimize when you’ve got millions of rows and you have specific access patterns you know you need to optimize for.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









