加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 相应的回滚是什么?

发布时间:2020-12-17 03:20:09 所属栏目:百科 来源:网络整理
导读:我有以下ActiveRecord迁移: class CreateSubjects ActiveRecord::Migration def self.up create_table :subjects do |t| t.string :title t.timestamps end change_table :projects do |t| t.references :subjects end end def self.down drop_table :subje
我有以下ActiveRecord迁移:

class CreateSubjects < ActiveRecord::Migration
  def self.up
    create_table :subjects do |t|
      t.string :title
      t.timestamps
    end

    change_table :projects do |t|
      t.references :subjects
    end
  end

  def self.down
    drop_table :subjects
    remove_column :projects,:subjects_id #defeats the purpose of having references
  end
end

我其实喜欢参考风格.不幸的是,我无法在self.down部分找到与回滚等效的回滚.如果我写remove_column:projects,:subjects_id我也可以写t.integer:subjects_id,这会让它更安全.

解决方法

它被称为 remove_references.

t.remove_references :subjects

小心! Rails按惯例使用单数,应该是:

def self.up
  create_table :subjects do |t|
    t.string :title
    t.timestamps
  end

  change_table :projects do |t|
    t.references :subject
  end
end

def self.down
  drop_table :subjects
  change_table :projects do |t|
    t.remove_references :subject
  end
end

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读