从表中删除belongs_to关系

时间:2015-10-21 11:01:28

标签: ruby-on-rails migration database-migration

我想从自定义礼服表中删除User belongs_to关系。

这是我定制的着装迁移:

class CreateCustomisedDresses < ActiveRecord::Migration
  def change
    create_table :customised_dresses do |t|
      t.belongs_to :user, index: true, null: :false
      t.timestamps null: false
    end
    add_foreign_key :customised_dresses, :users, dependent: :delete
end

我尝试了以下迁移,但它给了我一个错误:

class RemoveForeignKeyUsersFromCustomisedDresses < ActiveRecord::Migration
  def self.up
    add_foreign_key :customised_dresses, :users
  end
  def self.down
    remove_foreign_key :customised_dresses, :users
  end
end

1 个答案:

答案 0 :(得分:1)

您应该使用上下方法交换代码:

class RemoveForeignKeyUsersFromCustomisedDresses < ActiveRecord::Migration
  def self.up
    remove_foreign_key :customised_dresses, :users
  end
  def self.down
    add_foreign_key :customised_dresses, :users
  end
end