如何在 Rails 迁移中添加检查约束?
我需要向 Rails 应用程序中的现有表添加一个新的整数列。该列只能有值 1、2、3,因此我想向表/列添加检查约束。如何在 Rails 迁移中指定此约束?
I need to add a new integer column to an existing table in my Rails app. The column can only have values 1, 2, 3, so I'd like to add a check constraint to the table/column. How do I specify this constraint within a Rails migration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Rails 迁移不提供任何添加约束的方法,但您仍然可以通过迁移来完成此操作,但将实际的 SQL 传递给execute()
创建迁移文件:
现在,在迁移文件中:
Rails migration does not provide any way to add Constraints, but you can still do it via migration but by passing actual SQL to execute()
Create Migration file:
Now, in the migration file:
Rails 6.1+ 检查约束
Rails 6.1 添加了对数据库迁移检查约束的基本支持。
因此,现在,用于添加检查约束(将整数列值限制为 1、2 和 3)的迁移可以编写如下:
这里是 指向相关 PR 的链接,您可以在其中找到有关
add_check_constraint
和remove_check_constraint
。Rails 6.1+ Check Constraints
Rails 6.1 added basic support for check constraints to database migrations.
So now, a migration for adding a check constraint which restricts integer column values only to 1, 2, and 3 can be written as follows:
Here is a link to the relative PR where you can find more details about
add_check_constraint
andremove_check_constraint
.您可以使用迁移验证器 gem 来完成此操作。请在此处查看详细信息: https://github.com/vprokopchuk256/mv-core
有了这个宝石,你将能够在数据库级别定义包含验证:
此外,您还能够定义如何定义该验证,甚至应该显示错误消息:
您甚至可以从迁移到模型来升级该验证:
然后进行验证迁移中的定义也将被定义为模型中的 ActiveModel 验证:
You can do it with Migration Validators gem. See details here: https://github.com/vprokopchuk256/mv-core
With that gem you'll be able to define inclusion validation on db level:
moreover you is able to define how that validation should be defined and even error message that should be shown:
you can even level up that validation from migration right to your model:
and then validation defines in migration will be also defined as ActiveModel validation in your model:
截至 2021 年 5 月,这个答案已过时
我刚刚为此发布了一个 gem: active_record-postgres-限制。正如 README 所描述的,您可以将它与 db/ 一起使用schema.rb 文件,它在迁移中添加了对以下方法的支持:
请注意,目前仅支持 postgres。
This answer is obsolete as of May 2021
I just published a gem for this: active_record-postgres-constraints. As the README there describes, you can use it with a db/schema.rb file, and it adds support for the following methods in migrations:
Note that at this time, only postgres is supported.
我刚刚完成了 PostgreSQL CHECK 约束的工作。
Nilesh 的解决方案并不十分完整; db/schema.rb 文件不会包含约束,因此使用 db:setup 的测试和任何部署都不会获得约束。根据 http://guides.rubyonrails.org/migrations.html#架构转储类型
即,在 config/application.rb 中设置
不幸的是,如果您使用 PostgreSQL,则在加载结果转储时可能会收到错误,请参阅 错误:必须是语言 plpgsql 的所有者。我不想在讨论中深入探讨 PostgreSQL 配置路径;另外,无论如何我喜欢有一个可读的 db/schema.rb 文件。因此,这对我来说排除了迁移文件中的自定义 SQL。
Valera 建议的 https://github.com/vprokopchuk256/mv-core gem 似乎很有前途,但它只支持一组有限的约束(当我尝试使用它时出现错误,尽管这可能是由于与我包含的其他宝石不兼容)。
我采用的解决方案(黑客)是让模型代码插入约束。因为它有点像验证,所以这就是我所说的:
当然,这会在每次验证之前进行额外的选择;如果这是一个问题,解决方案是将其放入“连接后”猴子补丁中,例如 如何使用rails连接到oracle后运行特定脚本?(你不能简单地缓存选择的结果,因为验证/约束添加发生在可能会回滚的事务,因此您需要每次都进行检查。)
I have just worked through getting a PostgreSQL CHECK constraint to work.
Nilesh's solution is not quite complete; the db/schema.rb file won't include the constraint, so tests and any deployments that use db:setup won't get the constraint. As per http://guides.rubyonrails.org/migrations.html#types-of-schema-dumps
I.e., in config/application.rb set
Unfortunately, if you're using PostgreSQL you may get an error when loading the resultant dump, see discussion at ERROR: must be owner of language plpgsql. I didn't want to go down the PostgreSQL configuration path in that discussion; plus in any case i'm fond of having a readable db/schema.rb file. So that ruled out custom SQL in the migration file for me.
The https://github.com/vprokopchuk256/mv-core gem suggested by Valera seems promising, but it only supports a limited set of constraints (and I got an error when I tried to use it, though that may be due to incompatibilities with other gems I'm including).
The solution (hack) I went with is to have the model code insert the constraint. Since it's kindof like a validation, that's where I put it:
Of course this does an extra select before each validation; if that's a problem a solution would be to put it in an "after connect" monkey patch such as discussed in How to run specific script after connected to oracle using rails? (You can't simply cache the result of the select, because the validation/constraint addition happens within a transaction that might get rolled back, so you need to check each time.)
您可以使用
Sequel
gem https://github.com/jeremyevans/sequelYou can use
Sequel
gem https://github.com/jeremyevans/sequel