如何使用Rails将长文本存储到MySql DB? [英] How to store long text to MySql DB using Rails?

查看:58
本文介绍了如何使用Rails将长文本存储到MySql DB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将较长的文本(在我的情况下为原始rss提要,但也可能是较长的博客文章或类似内容)存储到MySql数据库中.

I am trying to store a long text (in my case a raw rss feed, but could just as well be a long blog post or similar) to a MySql database.

我要迁移:

change_column :contents, :description, :longtext

但这给出了一个schema.rb:

But this gives a schema.rb with:

t.text     "description",       :limit => 2147483647

实际上应该将限制设置为4294967295.

When the limit should in fact have been set to 4294967295.

为什么Rails施加的上限是应该达到的上限的一半?

Why does Rails impose an upper limit which is half of what should be possible?

推荐答案

我不知道rails是否在以前的版本中正式支持:longtext,但根据

I don't know if rails supported :longtext officially in previous versions, but in the current version according to rails documentation, :longtext is in fact not specified as legal datatype. I think it's only a convenience of the mysql adapter that translates this to :text.

那么正确的方法是:

change_column:contents,:description,:text,:limit => 4294967295

change_column :contents, :description, :text, :limit => 4294967295

请记住,使用多字节字符时,有效最大大小会减小.

Keep it mind that the effective maximum size is less when using multi-byte characters.

编辑:考虑一秒钟,将滑轨的尺寸减半是有意义的.重新阅读有关该主题的 mysql文档 ,他们说的是有效大小.我猜想指定2147483647时,如果填充2字节的UTF-8字符,可能会导致4294967295的有效大小.由于UTF-8是ruby 1.9中的默认编码(至少在我的机器上),因此这是唯一的正确方法.我认为吗?!

Thinking a second about it and it makes sense that rails halves the size. Re-reading the mysql docs on this topic , they speak of effective size. I guess specifying 2147483647 could lead to an effective size of 4294967295 when filled with 2-byte UTF-8 characters. As UTF-8 is the default encoding in ruby 1.9 (at least on my machine), it's the only correct way to do it. I think?!

ruby-1.9.2-p136 :002 > "".encoding
=> #<Encoding:UTF-8> 

这篇关于如何使用Rails将长文本存储到MySql DB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆