Ruby-on-rails 在 PostgreSQL 中强制字符串的最大长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2982827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:44:30  来源:igfitidea点击:

Force max length for string in PostgreSQL

ruby-on-railspostgresql

提问by Jimmy

I am using heroku for a RoR application and am trying to manually set the length of a string column and am having trouble.

我正在将 heroku 用于 RoR 应用程序,并尝试手动设置字符串列的长度,但遇到了问题。

I tried making a migration along the lines of

我尝试按照以下方式进行迁移

change_column :posts, :content, :string, :length => 10000

I assumed this would work but no such luck, anyone have some pointers?

我认为这会奏效,但没有这样的运气,有人有指点吗?

Thanks!

谢谢!

回答by Jason Weathered

The length limit option in Rails migrations is called :limit:

Rails 迁移中的长度限制选项称为:limit

change_column :posts, :content, :string, :limit => 10000

If you are finding yourself changing VARCHARlength limits a lot, you might want to read @depesz's blog post on VARCHARvs TEXT.

如果您发现自己经常更改VARCHAR长度限制,则可能需要阅读 @depesz 在VARCHARvsTEXT上的博客文章。