ruby-on-rails - 使用 Rails 5 丢失迁移文件的自定义代码

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

我的一个迁移文件消失了 🤷‍♂️ 我想我需要手动重写它。 😰

这是我在该表的 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

最佳答案

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

将迁移更改为,

  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

关于ruby-on-rails - 使用 Rails 5 丢失迁移文件的自定义代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49174773/

相关文章:

ruby-on-rails - Rails/Activerecord 数据库字段时区

sql - 多对多关系 : use associative table or delimited values in a column?

sql - 将 JSON 结果连接到单列 postgresql

sql - 在 PostgreSQL 中生成随机字符串

database - Flask Migrate 和 PostgreSQL 数据更新

javascript - 没有 CSRF token 的 React Native Fetch 请求失败

ruby-on-rails - RSpec:如何测试 includes().where() 中的参数是否正确

javascript - 插件/Gems/库以获得浏览器中代码的语法突出显示?

database - 数据库表中的动态列与 EAV

php - 转换 MySQL 表和行