在 Rails 迁移中,如何消除字段的限制

发布于 2024-09-14 01:01:17 字数 99 浏览 4 评论 0原文

下面的说法正确吗?

 change_column :tablename, :fieldname, :limit => null

Is the following correct?

 change_column :tablename, :fieldname, :limit => null

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

宫墨修音 2024-09-21 01:01:17

如果您之前在迁移中指定了限制,并且只想删除该限制,则可以执行以下操作:

change_column :users, :column, :string, :limit => 255

255 是字符串列的标准长度,rails 将删除您之前指定的限制。

更新:

虽然这适用于许多 Rails 版本,但您可能更适合使用 nil 就像 Giuseppe 的答案一样。

change_column :users, :column, :string, :limit => nil

这意味着您唯一做错的事情是使用 null 而不是 nil

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.

Updated:

While this works in a number of Rails versions, you would probably be better suited to use nil like in Giuseppe's answer.

change_column :users, :column, :string, :limit => nil

That means the only thing you were doing wrong was using null instead of nil.

卖梦商人 2024-09-21 01:01:17

这就是我身上发生的事情。

我意识到表中的字符串字段不足以保存其内容,因此我生成了一个包含以下内容的迁移:

def self.up
  change_column :articles, :author_list, :text
end

但是,在运行迁移之后,架构具有:

create_table "articles", :force => true do |t|
  t.string   "title"
  t.text     "author_list", :limit => 255
end

这不太好。然后我“重做”了迁移,如下所示:

def self.up
  # careful, it's "nil", not "null"
  change_column :articles, :author_list, :text, :limit => nil
end

这一次,schema.rb 中的限制消失了:

create_table "articles", :force => true do |t|
  t.string   "title"
  t.text     "author_list"
end

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:

create_table "articles", :force => true do |t|
  t.string   "title"
  t.text     "author_list"
end
舟遥客 2024-09-21 01:01:17

将列类型更改为 :text。它没有限制。

change_column :tablename, :fieldname, :text, :limit => nil

Change the column type to :text. It does not have a limit.

change_column :tablename, :fieldname, :text, :limit => nil
自此以后,行同陌路 2024-09-21 01:01:17

大多数数据库不支持无限制的字符串:您必须在 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.

不气馁 2024-09-21 01:01:17

为了使其独立于数据库驱动程序,应该这样写:

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

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
仅此而已 2024-09-21 01:01:17

今天我也是同样的情况,试图删除我添加到文本字段的限制,但没有成功。尝试了几次迁移。

导轨 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文