我的一个迁移文件消失了 🤷♂️ 我想我需要手动重写它。 😰
这是我在该表的 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/