LoginSignup
29
27

More than 5 years have passed since last update.

Railsのmodelで長い文字列を扱う時はstringではなくtextを使おう

Last updated at Posted at 2014-08-09

追記
Rails 4.2だとこの問題は解決されているようです。

Rails 4.2 で postgresql を使う場合の注意事項 - おもしろwebサービス開発日記

-

何かのトークンなんかを保存する時に、データ型をstringでやると、文字数が255より大きくになった時に死ぬ。

例えば、

rails generate User name:string token:string

みたいな感じでやると、

ActiveRecord::StatementInvalid: PG::StringDataRightTruncation: ERROR:  value too long for type character varying(255)

とか出てきて怒られた。(PG::StringDataRightTruncationとか出てきてるし、
PostgreSQLだけの話なのかもしれない。

ので、くっそ長い文字列をRailsで扱う時は、

rails generate User name:string token:text

みたいな感じで text でやってあげましょう。

29
27
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
27