(Rails - version 5.0.0, Ruby 2.3.0p0)
I want to create a relationship between the My Users table and Cards. I added belongs_to :userto the "Maps" has_many :cardsmodel and to the "Users" model and created a migration using:
class AddUserIdToCard < ActiveRecord::Migration[5.0]
def change
add_foreign_key :cards, :users, column: :user_id
end
end
When I start rake db:migrate, I get an error message:
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "user_id" referenced in foreign key constraint does not exist
: ALTER TABLE "cards" ADD CONSTRAINT "fk_rails_8ef7749967"
FOREIGN KEY ("user_id")
REFERENCES "users" ("id")
Now I first worked on this problem, just adding add_column :cards, :user_id, :integerto the transfer, but this is not very indicative, and I am worried about the problems that appear later. Is there a better way to do this?
source
share