如何在 Rails 3.1 中删除“引用”列?
我在之前的迁移中创建了一个包含 references
列的表,现在我想删除它。我知道我可以对生成的名称调用 remove_column
,但是有没有办法使用表名称来删除它?
remove_references :blah, :users
而不是 remove_column :blah, :user_id
I've created a table with a references
column in an earlier migration and now I would like to drop it. I know I can do a call to remove_column
on the generated name, but is there a way to remove it using the the table name instead?
remove_references :blah, :users
instead of remove_column :blah, :user_id
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所猜测的,有一个方法被恰当地命名为 remove_references 。它只需要一个参数,就像
references
也需要一个参数:来自 API 文档:
There is the method aptly named remove_references, as you guessed. It only needs one parameter, same as
references
needs one parameter too:From the API documentation:
没有特殊的方法可以做到这一点。当您考虑这一点时,这是完全有道理的,因为
references
方法实际上并没有在数据库中创建外部约束,这意味着这个生成的列根本不特殊。更新
正如@Zabba指出的那样,似乎有一个 remove_references 方法。我从未见过它在实践中使用过,但它的描述听起来很正确。
There is no special method to do so. When you come to think about this it makes perfect sense, since the
references
method doesn't actually create a foreign constraint in the db, meaning that this generated column isn't special at all.Update
As @Zabba noted there seems to be a remove_references method. I've never seen it used in practice, but its description sounds about right.