Rails:去除引用关系
Rails: remove reference relationship
我对教程中的以下代码感到困惑。
目标是从 table books
中删除引用键 genre_id
class RemoveGenreFromBooks < ActiveRecord::Migration
def up
remove_index :books, column: [:genre_id]
remove_column :books, :genre_id
end
def down
add_reference :books, :genre, index: true
end
end
但是我不明白remove_index :books, column: [:genre_id]
是什么意思
此外,我在down
方法中没有得到index: true
。
如果我需要添加关系,为什么我不能直接输入
class Addrelationship < ActiveRecord::Migration
def change
add_reference :books, :genre
end
耙子db:rollback步数=1
如果要回滚的迁移是最后应用的迁移,这是一种执行此操作的方法。您可以用 1 代替您想返回的任意多个迁移。
因为有一种添加引用的方法,所以也有一种方法可以删除 - remove_reference
语法为:remove_reference(table_name, ref_name, options = {})
所以在你的情况下,删除 Genre
的引用:
class RemoveGenreFromBooks < ActiveRecord::Migration
def change
remove_reference :books, :genre, index:true, foreign_key: true
end
end
选项 foreign_key: true
还将从 books
table 中删除外键。
我对教程中的以下代码感到困惑。
目标是从 table books
genre_id
class RemoveGenreFromBooks < ActiveRecord::Migration
def up
remove_index :books, column: [:genre_id]
remove_column :books, :genre_id
end
def down
add_reference :books, :genre, index: true
end
end
但是我不明白remove_index :books, column: [:genre_id]
是什么意思
此外,我在down
方法中没有得到index: true
。
如果我需要添加关系,为什么我不能直接输入
class Addrelationship < ActiveRecord::Migration
def change
add_reference :books, :genre
end
耙子db:rollback步数=1
如果要回滚的迁移是最后应用的迁移,这是一种执行此操作的方法。您可以用 1 代替您想返回的任意多个迁移。
因为有一种添加引用的方法,所以也有一种方法可以删除 - remove_reference
语法为:remove_reference(table_name, ref_name, options = {})
所以在你的情况下,删除 Genre
的引用:
class RemoveGenreFromBooks < ActiveRecord::Migration
def change
remove_reference :books, :genre, index:true, foreign_key: true
end
end
选项 foreign_key: true
还将从 books
table 中删除外键。