在 Rails 迁移中,如何消除字段的限制
下面的说法正确吗?
change_column :tablename, :fieldname, :limit => null
Is the following correct?
change_column :tablename, :fieldname, :limit => null
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您之前在迁移中指定了限制,并且只想删除该限制,则可以执行以下操作:
255 是字符串列的标准长度,rails 将删除您之前指定的限制。
更新:
虽然这适用于许多 Rails 版本,但您可能更适合使用
nil
就像 Giuseppe 的答案一样。这意味着您唯一做错的事情是使用
null
而不是nil
。If you previously specified a limit in a migration and want to just remove the limit, you can just do this:
255 is the standard length for a string column, and rails will just wipe out the limit that you previously specified.
Updated:
While this works in a number of Rails versions, you would probably be better suited to use
nil
like in Giuseppe's answer.That means the only thing you were doing wrong was using
null
instead ofnil
.这就是我身上发生的事情。
我意识到表中的字符串字段不足以保存其内容,因此我生成了一个包含以下内容的迁移:
但是,在运行迁移之后,架构具有:
这不太好。然后我“重做”了迁移,如下所示:
这一次,schema.rb 中的限制消失了:
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:
After running the migration, however, the schema had:
Which was not OK. So then I "redid" the migration as follows:
This time, the limit was gone in schema.rb:
将列类型更改为
:text
。它没有限制。Change the column type to
:text
. It does not have a limit.大多数数据库不支持无限制的字符串:您必须在
varchar(SIZE)
定义中指定大小。虽然你可以尝试,但我个人会选择
:limit => BIG_ENOUGH_NUMBER
。您还可以考虑对非常大的文本使用 CLOB 类型。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.为了使其独立于数据库驱动程序,应该这样写:
To make it db-driver-independent one should write smth like this:
今天我也是同样的情况,试图删除我添加到文本字段的限制,但没有成功。尝试了几次迁移。
导轨 4.2.7.1
Ruby 2.3.1p112
最后,唯一有效的方法是将限制指定为 255。尝试调整其他任何内容对我来说都不起作用。
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
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.