【问题标题】:Custom code for a lost migration file with Rails 5使用 Rails 5 丢失迁移文件的自定义代码
【发布时间】:2018-03-08 13:52:24
【问题描述】:

我的一个迁移文件消失在蓝天 ????‍♂️ 我想我需要手动重写它。 ????

这是我在 schema.rb 中为该表提供的内容

 +  create_table "collections", force: :cascade do |t|
 +    t.string "title"
 +    t.string "description"
 +    t.bigint "designer_id"
 +    t.datetime "created_at", null: false
 +    t.datetime "updated_at", null: false
 +    t.index ["designer_id"], name: "index_collections_on_designer_id"
 +  end

那么我的自定义迁移应该如下所示吗?

  def change
    create_table :collections do |t|
      t.string :title
      t.string :description
      t.integer :designer_id

      t.timestamps
    end
  end

我应该对 add_index 做些什么吗?

谢谢!

------------

编辑的解决方案想法

我将添加到现有的迁移文件,而不是创建新文件。所以下面的代码应该可以完成这项工作,对吧?

class AddSlugToCollections < ActiveRecord::Migration[5.1]
  def change
    create_table :collections do |t|
      t.string :title
      t.string :description
      t.integer :designer_id

      t.timestamps
    end
    add_index :collections, :designer_id

    add_column :collections, :slug, :string
    add_index :collections, :slug
  end
end

【问题讨论】:

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


    【解决方案1】:

    是的,如果您想再次保留相同的更改,那么您也应该添加索引。

    将迁移更改为,

      def change
        create_table :collections do |t|
            t.string :title
          t.string :description
          t.integer :designer_id
    
          t.timestamps
        end
        add_index :collections, :designer_id
      end
    

    【讨论】:

    • 谢谢伙计。我用你的建议更新了我的问题。我将尝试将其添加到现有的迁移文件中。我猜应该没问题。
    【解决方案2】:

    @ganesh Navale 是正确的。我想在这方面再补充一点。如果您的迁移已经运行,则将新的迁移时间戳重命名为旧的迁移时间戳。您可以从 rake db:migrate:status 命令获取旧的迁移时间戳。

    【讨论】:

    • 啊,喜欢命令行提供的东西!谢谢!顺便说一句,我丢失了那个迁移文件真是太奇怪了。我制作了一个脚手架集合,它在我的本地主机上运行良好,但是当我尝试部署到 digitalocean 时,我错过了创建集合并出现错误
    猜你喜欢
    • 1970-01-01
    • 2021-09-18
    • 2022-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多