如何创建 Rails 迁移以使用删除级联约束更新外键?
How do I create a Rails migration that updates a foreign key with an on-delete cascade constraint?
我正在使用 Rails 4.2.3 和 PostgreSQL 数据库。我想编写一个迁移来更新我的一个外键以具有删除级联约束,因此我创建了以下内容:
class UpdateForeignKeyAddOnDeleteConstraint < ActiveRecord::Migration
def change
remove_foreign_key :my_object_times, :my_objects
add_foreign_key :my_object_times, :my_objects, on_delete: cascade
end
end
但是当我 运行 迁移时,出现以下错误:
$ rake db:migrate
== 20160525203028 UpdateForeignKeyAddOnDeleteConstraint: migrating ============
-- remove_foreign_key(:my_object_times, :my_objects)
-> 0.0454s
-- cascade()
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
undefined local variable or method `cascade' for #<UpdateForeignKeyAddOnDeleteConstraint:0x007f82f2c71998>
/Users/davea/.rvm/gems/ruby-2.3.0@global/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:664:in `block in method_missing'
/Users/davea/.rvm/gems/ruby-2.3.0@global/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:634:in `block in say_with_time'
/Users/davea/.rvm/gems/ruby-2.3.0@global/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:634:in `say_with_time'
/Users/davea/.rvm/gems/ruby-2.3.0@global/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:654:in `method_missing'
我应该如何编写迁移来更新外键?
您需要更改此行,
add_foreign_key :my_object_times, :my_objects, on_delete: cascade
有了这个:
add_foreign_key :my_object_times, :my_objects, on_delete: :cascade
简单的区别就是cascade
应该是一个符号(:cascade
)或者一个字符串('cascade'
)。
有关详细信息,请参阅 documentation on add_foreign_key。
希望对您有所帮助。
我正在使用 Rails 4.2.3 和 PostgreSQL 数据库。我想编写一个迁移来更新我的一个外键以具有删除级联约束,因此我创建了以下内容:
class UpdateForeignKeyAddOnDeleteConstraint < ActiveRecord::Migration
def change
remove_foreign_key :my_object_times, :my_objects
add_foreign_key :my_object_times, :my_objects, on_delete: cascade
end
end
但是当我 运行 迁移时,出现以下错误:
$ rake db:migrate
== 20160525203028 UpdateForeignKeyAddOnDeleteConstraint: migrating ============
-- remove_foreign_key(:my_object_times, :my_objects)
-> 0.0454s
-- cascade()
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
undefined local variable or method `cascade' for #<UpdateForeignKeyAddOnDeleteConstraint:0x007f82f2c71998>
/Users/davea/.rvm/gems/ruby-2.3.0@global/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:664:in `block in method_missing'
/Users/davea/.rvm/gems/ruby-2.3.0@global/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:634:in `block in say_with_time'
/Users/davea/.rvm/gems/ruby-2.3.0@global/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:634:in `say_with_time'
/Users/davea/.rvm/gems/ruby-2.3.0@global/gems/activerecord-4.2.5.1/lib/active_record/migration.rb:654:in `method_missing'
我应该如何编写迁移来更新外键?
您需要更改此行,
add_foreign_key :my_object_times, :my_objects, on_delete: cascade
有了这个:
add_foreign_key :my_object_times, :my_objects, on_delete: :cascade
简单的区别就是cascade
应该是一个符号(:cascade
)或者一个字符串('cascade'
)。
有关详细信息,请参阅 documentation on add_foreign_key。
希望对您有所帮助。