ddb
ddb

Reputation: 1406

is there a limit for number of characters in active record (mysql db) text field

Here is my schema :

create_table "delayed_jobs", :force => true do |t|
    t.integer  "priority",   :default => 0
    t.integer  "attempts",   :default => 0
    t.text     "handler"
    t.text     "last_error"
    t.datetime "run_at"
    t.datetime "locked_at"
    t.datetime "failed_at"
    t.string   "locked_by"
    t.string   "queue"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

I am using delayed job for emails. One of my emails has a very long handler (50000+) characters and this happens to be cut off while it is saved in the handler text field. How many characters can a text field typically take and how should I increase it?

Upvotes: 3

Views: 3291

Answers (3)

cash22
cash22

Reputation: 450

I had a similar problem to this in my messages model. Initially when creating the model the message body was defined as string

create_table :messages do |t|
  t.string :body, 
......
end

This truncated my message body to 256 characters. So I changed that column with another migration:

def up
  change_table :messages do |t|
    t.change :body, :text
  end
end

As mhasan outlined in his answer, :text is 65535 bytes long. If you need longer you can use :mediumtext or :longtext instead

Upvotes: 2

Bohdan
Bohdan

Reputation: 414

As far I know Ruby by itself doesn't have any limits for the length of strings, so strings you store in Rails are limited by your database capabilities.

Upvotes: 1

Mudassir Hasan
Mudassir Hasan

Reputation: 28741

MySQL supports 4 TEXT field types

(TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT)

The limits for each type is

> TINYTEXT  256 bytes     TEXT  65,535 bytes     MEDIUMTEXT  16,777,215 bytes   
> LONGTEXT  4,294,967,295 bytes

You can change accordingly. Note : Size in bytes is number of chars

Upvotes: 6

Related Questions