53

Is the following correct?

 change_column :tablename, :fieldname, :limit => null
simonmorley
  • 2,810
  • 4
  • 30
  • 61
kidbrax
  • 2,364
  • 3
  • 30
  • 38

6 Answers6

111

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.

Jeremy Baker
  • 3,986
  • 3
  • 24
  • 27
35

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
Giuseppe
  • 5,188
  • 4
  • 40
  • 37
  • For me I needed rake db:migrate:reset to really change the limit, and one should be careful about that command, cause it's drops the database. – p1100i Nov 08 '12 at 17:07
3

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

change_column :tablename, :fieldname, :text, :limit => nil
edgerunner
  • 14,873
  • 2
  • 57
  • 69
  • 1
    this is incorrect... if you simply change from :string to :text, rake db:migrate will leave the limit in place, which will mess things up when you try to do heroku rake db:schema:load – jpw Feb 02 '11 at 22:40
0

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.

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
0

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
0

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.

GiantCoder
  • 31
  • 1