I have a join_table that currently has the following columns:
- id
- table_a_id
- table_b_id
Attempting to add a foreign key constraint to join_table for table_a_id, I generate the following migration:
class AddTableIdForeignKeyConstraintToJoinTable < ActiveRecord::Migration[5.0]
def change
add_foreign_key :table_a, :join_table, column: :table_a_id, primary_key: "lng_id"
end
end
Error:
PG::UndefinedColumn: ERROR: column "question_id" referenced in
foreign key constraint does not exist
: ALTER TABLE "table_a" ADD CONSTRAINT
"fk_rails_4b0148d527"
FOREIGN KEY ("question_id")
REFERENCES "join_table" ("lng_id")
Questions
What does this line mean foreign key constraint does not exist? Where is Rails looking for the foreign key?
table_bto the join table and getting the error thatconstraint "fk_rails_ff820b2696" for relation "join_table" already exists. This means that the methodadd_foreign_keydoes not do what I think it does. So now working on understanding.