Ruby-on-rails 在 Rails 迁移中,如何删除字段的限制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3484249/
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
In a rails migration, how can you remove the limit of a field
提问by kidbrax
Is the following correct?
以下是否正确?
change_column :tablename, :fieldname, :limit => null
回答by Jeremy Baker
If you previously specified a limit in a migration and want to just remove the limit, you can just do this:
如果您之前在迁移中指定了限制并且只想删除限制,您可以这样做:
change_column :users, :column, :string, :limit => 255
255 is the standard length for a string column, and rails will just wipe out the limit that you previously specified.
255 是字符串列的标准长度,而 rails 只会消除您之前指定的限制。
Updated:
更新:
While this works in a number of Rails versions, you would probably be better suited to use nillike in Giuseppe's answer.
虽然这适用于许多 Rails 版本,但您可能更适合使用nilGiuseppe 的回答。
change_column :users, :column, :string, :limit => nil
That means the only thing you were doing wrong was using nullinstead of nil.
这意味着你唯一做错的是使用null而不是nil.
回答by Giuseppe
Here's what happened to me.
这是发生在我身上的事情。
I realized that a string field I had in a table was not sufficient to hold its content, so I generated a migration that contained:
我意识到我在表中的字符串字段不足以保存其内容,因此我生成了一个包含以下内容的迁移:
def self.up
change_column :articles, :author_list, :text
end
After running the migration, however, the schema had:
但是,在运行迁移后,架构具有:
create_table "articles", :force => true do |t|
t.string "title"
t.text "author_list", :limit => 255
end
Which was not OK. So then I "redid" the migration as follows:
这不正常。那么我“重新”迁移如下:
def self.up
# careful, it's "nil", not "null"
change_column :articles, :author_list, :text, :limit => nil
end
This time, the limit was gone in schema.rb:
这一次,schema.rb 中的限制消失了:
create_table "articles", :force => true do |t|
t.string "title"
t.text "author_list"
end
回答by edgerunner
Change the column type to :text. It does not have a limit.
将列类型更改为:text。它没有限制。
change_column :tablename, :fieldname, :text, :limit => nil
回答by Nikita Rybak
Strings without limit is not something most databases support: you have to specify size in varchar(SIZE)definition.
Although you could try, I would personally go with :limit => BIG_ENOUGH_NUMBER. You may also consider using CLOB type for very big texts.
大多数数据库不支持无限制的字符串:您必须在varchar(SIZE)定义中指定大小。
虽然你可以尝试,但我个人会选择:limit => BIG_ENOUGH_NUMBER. 您也可以考虑对非常大的文本使用 CLOB 类型。
回答by user420577
To make it db-driver-independent one should write smth like this:
为了使它与数据库驱动程序无关,应该这样写:
add_column :tablename, :fieldname_tmp, :text
Tablename.reset_column_information
Tablename.update_all("fieldname_tmp = fieldname")
remove_column :tablename, :fieldname
rename_column :tablename, :fieldname_tmp, :fieldname
回答by GiantCoder
I was the same boat today, trying to remove a limit I'd added to a text field and it wouldn't take. Tried several migrations.
我今天也是一样,试图删除我添加到文本字段的限制,但它不会。尝试了几次迁移。
Rails 4.2.7.1 Ruby 2.3.1p112
Rails 4.2.7.1 红宝石 2.3.1p112
In the end, the only thing that worked was specifying a limit of 255. Trying to adjust to anything else wouldn't work for me.
最后,唯一有效的方法是指定 255 的限制。试图适应其他任何东西对我来说都行不通。

