Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between string and text in rails?

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

like image 990
Mo. Avatar asked Jul 28 '10 15:07

Mo.


People also ask

What is the difference between varchar and text in postgresql?

Some Differences Between VARCHAR and TEXT While both data types share a maximum length of 65,535 characters, there are still a few differences: The VAR in VARCHAR means that you can set the max size to anything between 1 and 65,535. TEXT fields have a fixed max size of 65,535 characters.

What is varchar in Ruby?

Varchar is usually limited to a maximum length of 255 (in MySQL, other RDBMSs have similar limits). If you use Rails' form helpers, a textarea will be output for this field (because it is of type 'text'). textarea is the form element that accepts multi-line input.

What is the size of text in MySQL?

TEXT: 65,535 characters - 64 KB The standard TEXT data object is sufficiently capable of handling typical long-form text content. TEXT data objects top out at 64 KB (expressed as 2^16 -1) or 65,535 characters and requires a 2 byte overhead.

What is a Rails migration?

A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.


2 Answers

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)

  • https://edgeguides.rubyonrails.org/active_record_migrations.html
:string |                   VARCHAR                | :limit => 1 to 255 (default = 255)   :text   | TINYTEXT, TEXT, MEDIUMTEXT, or LONGTEXT2 | :limit => 1 to 4294967296 (default = 65536) 

Reference:

https://hub.packtpub.com/working-rails-activerecord-migrations-models-scaffolding-and-database-completion/

When should each be used?

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

like image 186
TJ Koblentz Avatar answered Oct 07 '22 15:10

TJ Koblentz


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

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

PostsgreSQL manual

like image 28
Omar Qureshi Avatar answered Oct 07 '22 17:10

Omar Qureshi