0

I have recently discovered that a relationship between two of my tables is wrong and I would like to make the necessary changes - this involves dropping a foreign key on my accommodations table and adding a foreign key on my users table.

I have created a migration file using:

rails g migration ForeignKeyAdjustment

and then added this to the new migration file:

class ForeignKeyAdjustment < ActiveRecord::Migration
  def self.up
    remove_column :accommodations, :user_id
    add_column :users, :accommodation_id, :integer
  end
end

I then ran the migration using:

bundle exec rake db:migrate

and then used the console to check the column names in each table and the changes have not taken effect!

rails console

and

User.column_names

What am I missing?

p.s. would it be a major issue if I just deleted my models and the database migration files and generated new models from the terminal? It wouldn't take me long and no important data to consider.

4

1 回答 1

0

您是否建立了 Active Record 协会?这将允许您将记录从一个表映射到另一个表。否则,列名只是表中的任意名称。

您是否将外键属性添加到模型中?这将通过 attr 可访问的方式为现有模型创建属性

这些可能是一些潜在的问题

协会指南 http://guides.rubyonrails.org/association_basics.html

我还将问题的标题编辑为“迁移外键”之类的内容,以便其他人可以找到类似问题的答案。

于 2013-09-03T20:34:12.107 回答