Ruby-on-rails rails中的字符串和文本之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3354330/
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:58:18  来源:igfitidea点击:

Difference between string and text in rails?

ruby-on-rails

提问by Mo.

I'm making a new web app using Rails, and was wondering, what's the difference between stringand text? And when should each be used?

我正在使用Rails的一个新的Web应用程序,并想知道,有什么之间的区别stringtext?什么时候应该使用?

回答by TJ Koblentz

The difference relies in how the symbol is converted into its respective column type in query language.

区别在于符号如何转换为查询语言中的相应列类型。

with MySQL :string is mapped to VARCHAR(255) - http://guides.rubyonrails.org/migrations.html

使用 MySQL :string 映射到 VARCHAR(255) - http://guides.rubyonrails.org/migrations.html

:string |                   VARCHAR                | :limit => 1 to 255 (default = 255)  
:text   | TINYTEXT, TEXT, MEDIUMTEXT, or LONGTEXT2 | :limit => 1 to 4294967296 (default = 65536)

Reference:

参考:

http://www.packtpub.com/article/Working-with-Rails-ActiveRecord-Migrations-Models-Scaffolding-and-Database-Completion

http://www.packtpub.com/article/Working-with-Rails-ActiveRecord-Migrations-Models-Scaffolding-and-Database-Completion

When should each be used?

每个应该什么时候使用?

As a general rule of thumb, use :stringfor short text input (username, email, password, titles, etc.) and use :textfor longer expected input such as descriptions, comment content, etc.

作为一般经验法则,:string用于短文本输入(用户名、电子邮件、密码、标题等)并:text用于较长的预期输入,例如描述、评论内容等。

回答by Omar Qureshi

If you are using postgres use text wherever you can, unless you have a size constraint since there is no performance penalty for text vs varchar

如果您使用 postgres,请尽可能使用文本,除非您有大小限制,因为 text 与 varchar 没有性能损失

There is no performance difference among these three types, apart from increased storage space when using the blank-padded type, and a few extra CPU cycles to check the length when storing into a length-constrained column. While character(n) has performance advantages in some other database systems, there is no such advantage in PostgreSQL; in fact character(n) is usually the slowest of the three because of its additional storage costs. In most situations text or character varying should be used instead

除了使用空白填充类型时增加的存储空间以及存储到长度受限列时检查长度的一些额外 CPU 周期之外,这三种类型之间没有性能差异。虽然 character(n) 在其他一些数据库系统中具有性能优势,但在 PostgreSQL 中没有这样的优势;事实上 character(n) 通常是三个中最慢的,因为它有额外的存储成本。在大多数情况下,应改用文本或字符变化

PostsgreSQL manual

PostsgreSQL 手册

回答by berkes

String translates to "Varchar" in your database, while text translates to "text". A varchar can contain far less items, a text can be of (almost) any length.

字符串在您的数据库中转换为“Varchar”,而文本则转换为“文本”。varchar 可以包含更少的项目,文本可以是(几乎)任何长度。

For an in-depth analysis with good references check http://www.pythian.com/news/7129/text-vs-varchar/

有关具有良好参考资料的深入分析,请查看http://www.pythian.com/news/7129/text-vs-varchar/

Edit:Some database engines can load varcharin one go, but store text (and blob) outside of the table. A SELECT name, amount FROM productscould, be a lot slower when using textfor namethan when you use varchar. And since Rails, by default loads records with SELECT * FROM...your text-columns will be loaded. This will probably never be a real problem in your or my app, though (Premature optimization is ...). But knowing that text is not always "free" is good to know.

编辑:某些数据库引擎可以varchar一次性加载,但将文本(和 blob)存储在表之外。ASELECT name, amount FROM products可以,使用textfor 时name比使用时慢很多varchar。由于 Rails,默认情况下会加载带有SELECT * FROM...文本列的记录。不过,这在您或我的应用程序中可能永远不会成为真正的问题(过早优化是......)。但是知道文本并不总是“免费”是件好事。

回答by Gurudath BN

String if the size is fixed and small and text if it is variable and big. This is kind of important because text is way bigger than strings. It contains a lot more kilobytes.

如果大小固定且小,则为字符串;如果大小可变且为大,则为文本。这很重要,因为文本比字符串大得多。它包含更多的千字节。

So for small fields always use string(varchar). Fields like. first_name, login, email, subject (of a article or post) and example of texts: content/body of a post or article. fields for paragraphs etc

所以对于小字段总是使用 string(varchar)。田等。名字、登录名、电子邮件、主题(文章或帖子的)和文本示例:帖子或文章的内容/正文。段落等字段

String size 1 to 255 (default = 255)

字符串大小 1 到 255(默认值 = 255)

Text size 1 to 4294967296 (default = 65536)2

文本大小 1 到 4294967296(默认值 = 65536)2

回答by Ravi D

As explained above not just the db datatype it will also affect the view that will be generated if you are scaffolding. string will generate a text_field text will generate a text_area

如上所述,不仅仅是 db 数据类型,它还会影响如果您是脚手架将生成的视图。string 将生成一个 text_field text 将生成一个 text_area

回答by user2012677

Use string for shorter field, like names, address, phone, company

对较短的字段使用字符串,如姓名、地址、电话、公司

Use Text for larger content, comments, content, paragraphs.

将文本用于更大的内容、评论、内容、段落。

My general rule, if it's something that is more than one line, I typically go for text, if it's a short 2-6 words, I go for string.

我的一般规则是,如果内容超过一行,我通常会选择文本,如果是 2-6 个简短的单词,我会选择字符串。

The official rule is 255 for a string. So, if your string is more than 255 characters, go for text.

对于字符串,官方规则是 255。因此,如果您的字符串超过 255 个字符,请选择文本。

回答by Luis

If you are using oracle... STRINGwill be created as VARCHAR(255)column and TEXT, as a CLOB.

如果您使用 oracle...STRING将被创建为VARCHAR(255)列和TEXT, 作为CLOB.

NATIVE_DATABASE_TYPES = {
    primary_key: "NUMBER(38) NOT NULL PRIMARY KEY",
    string: { name: "VARCHAR2", limit: 255 },
    text: { name: "CLOB" },
    ntext: { name: "NCLOB" },
    integer: { name: "NUMBER", limit: 38 },
    float: { name: "BINARY_FLOAT" },
    decimal: { name: "DECIMAL" },
    datetime: { name: "TIMESTAMP" },
    timestamp: { name: "TIMESTAMP" },
    timestamptz: { name: "TIMESTAMP WITH TIME ZONE" },
    timestampltz: { name: "TIMESTAMP WITH LOCAL TIME ZONE" },
    time: { name: "TIMESTAMP" },
    date: { name: "DATE" },
    binary: { name: "BLOB" },
    boolean: { name: "NUMBER", limit: 1 },
    raw: { name: "RAW", limit: 2000 },
    bigint: { name: "NUMBER", limit: 19 }
}

https://github.com/rsim/oracle-enhanced/blob/master/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb

https://github.com/rsim/oracle-enhanced/blob/master/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb

回答by nategurutech

The accepted answer is awesome, it properly explains the difference between string vs text (mostly the limit size in the database, but there are a few other gotchas), but I wanted to point out a small issue that got me through it as that answer didn't completely do it for me.

接受的答案很棒,它正确地解释了字符串与文本之间的区别(主要是数据库中的限制大小,但还有一些其他问题),但我想指出一个让我通过它作为答案的小问题没有完全为我做。

The max size :limit => 1 to 4294967296didn't work exactly as put, I needed to go -1 from that max size. I'm storing large JSON blobs and they might be crazy huge sometimes.

最大尺寸:limit => 1 到 4294967296并没有完全按照放置的方式工作,我需要从最大尺寸变为 -1。我正在存储大型 JSON blob,有时它们可​​能会非常大。

Here's my migration with the larger value in place with the value MySQL doesn't complain about.

这是我的迁移,具有较大的价值,MySQL 没有抱怨的价值。

Note the 5at the end of the limit instead of 6

注意限制末尾的5而不是6

class ChangeUserSyncRecordDetailsToText < ActiveRecord::Migration[5.1]
  def up
    change_column :user_sync_records, :details, :text, :limit => 4294967295
  end

  def down
    change_column :user_sync_records, :details, :string, :limit => 1000
  end
end

回答by CodingBingo

If the attribute is matching f.text_fieldin form use string, if it is matching f.text_areause text.

如果属性f.text_field在表单中匹配使用string,如果它匹配f.text_area使用text