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.
mu is too short Jan 01 '11 at 17:43 2012-01-01 17:43
source share