Changing a column type to longer rows in rails

During the first migration, I declared the Activerecord row in the content column, made it the row (255) in accordance with the annotated gem.

After I click on an application on heroku that uses postgres, if I enter the form in the content line longer than 255, I get an error

 PGError: ERROR: value too long for type character varying(255) 

The problem is that the content must contain a string that is extremely long (free text, maybe thousands of characters)

  • Which variable (the string is not suitable for this) could pg accept?
  • How to create a migration to replace the type of this column

thank

+81
string ruby-on-rails postgresql rails-activerecord
Jan 01 '11 at 16:56
source share
2 answers

You should use text with Rails if you want a string with no length limit. Migration:

 def up change_column :your_table, :your_column, :text end def down # This might cause trouble if you have strings longer # than 255 characters. change_column :your_table, :your_column, :string end 

must understand. You may want to :null => false or some other options at the end of this.

When you use a string column without an explicit restriction, Rails will add an implicit :limit => 255 . But if you use text , you will get any arbitrary type of length string supported by the database. PostgreSQL allows you to use a varchar column without length, but most databases use a separate type for this, and Rails does not know about varchar without length. You must use text in Rails to get the text column in PostgreSQL. There is no difference in PostgreSQL between a column of type text and one of type varchar (but varchar(n) is different). In addition, if you are deploying on top of PostgreSQL, there is no reason to use :string (AKA varchar ) in general, the database processes text and varchar(n) the same inside, with the exception of additional length restrictions for varchar(n) ; you should use varchar(n) (AKA :string ) if you have an external constraint (for example, a government form that states that field 432 on form 897 / B will contain 23 characters) to fit the column.

As an aside, if you use the string column anywhere, you should always specify :limit as a reminder to yourself that there is a limit, and you should have validation in the model to make sure that the limit is not exceeded. If you exceed the limit, PostgreSQL will complain and raise an exception, MySQL will calmly truncate the row or complain (depending on the server configuration), SQLite will let it go as it is, and other databases will do something else (maybe complain).

In addition, you must also develop, test and deploy on top of the same database (which will usually be PostgreSQL in Heroku), you must use the same database server versions. There are other differences between databases (such as GROUP BY behavior) with which ActiveRecord will not isolate you. You may already be doing this, but I thought I would write about it anyway.

+190
Jan 01 '11 at 17:43
source share

While the accepted answer is excellent, I would like to add an answer here that I hope will be better off dealing with the original part of question 2, rather than experts like me.

  1. How to create a migration to replace the type of this column

forest migration generation

You can generate a migration to save the changes by entering your console (just replace table for your table name and column for column name)

 rails generate migrate change_table_column 

This will result in skeletal migration within you Rails application / db / migrate / folder. This migration is a placeholder for your migration code.

For example, I want to create a migration to change the column type from string to text , in a table called TodoItems:

 class ChangeTodoItemsDescription < ActiveRecord::Migration def change # enter code here change_column :todo_items, :description, :text end end 

Migration

After you have entered the code to change only the column being launched:

 rake db:migrate 

To apply the migration. If you make a mistake, you can always revert the change with:

 rake db:rollack 

Up and Down Methods

Accepted answers refer to the Up and Down methods instead of the new Change method. Since rails 3.2 , the old Up and Down methods brought several advantages over the new Change method. Up and down avoid ActiveRecord::IrreversibleMigration exception . Starting with the release of Rails 4, you can use reversible to avoid this error:

 class ChangeProductsPrice < ActiveRecord::Migration def change reversible do |dir| change_table :products do |t| dir.up { t.change :price, :string } dir.down { t.change :price, :integer } end end end end 

Enjoy Rails :)

0
Jun 10 '17 at 13:20
source share



All Articles