Marine
Issue with a `many_to_many` assoc and `join_keys` using the join table way
I’m trying to set up the usual many-to-many relationship between two schemas: User and BreadDay. However, the primary key on the bread_day table is day rather than id.
This Problem seemed the same as this post Issue with a `many_to_many` assoc and `join_keys` except that I am not joining the tables through a schema but with a join table.
What I have so far is the following:
#bread_day.ex
@primary_key {:day, :date, autogenerate: false}
schema “bread_days” do
many_to_many :users, User,
join_through: “user_bread_days”,
join_keys: [bread_day_day: :day, user_id: :id]
timestamps(type: :utc_datetime)
end
#user.ex
schema “users” do
field :email, :string
field :password, :string, virtual: true, redact: true
field :hashed_password, :string, redact: true
field :confirmed_at, :naive_datetime
many_to_many :bread_days, BreadDay,
join_through: "user_bread_days",
join_keys: [user_id: :id, bread_day_day: :day]
timestamps(type: :utc_datetime)
end
the join table in the migrations:
def change do
create table(“user_bread_days”, primary_key: false) do
add :user_id, references(:users, on_delete: :delete_all)
add :bread_day_day, references(:bread_days, on_delete: :delete_all)
end
and this is the error :
** (Postgrex.Error) ERROR 42703 (undefined_column) Column “id”, which is used in the foreign key, does not exist
(ecto_sql 3.11.0) lib/ecto/adapters/sql.ex:1054: Ecto.Adapters.SQL.raise_sql_call_error/1
(elixir 1.15.6) lib/enum.ex:1693: Enum.“-map/2-lists^map/1-1-”/2
(ecto_sql 3.11.0) lib/ecto/adapters/sql.ex:1161: Ecto.Adapters.SQL.execute_ddl/4
(ecto_sql 3.11.0) lib/ecto/migration/runner.ex:348: Ecto.Migration.Runner.log_and_execute_ddl/3
(elixir 1.15.6) lib/enum.ex:1693: Enum.“-map/2-lists^map/1-1-”/2
(ecto_sql 3.11.0) lib/ecto/migration/runner.ex:311: Ecto.Migration.Runner.perform_operation/3
(stdlib 5.1.1) timer.erl:270: :timer.tc/2
(ecto_sql 3.11.0) lib/ecto/migration/runner.ex:25: Ecto.Migration.Runner.run/8
Any Idea
?
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









