开发者

storing more than 255 characters in a PostgreSQL DB on heroku

I am using heroku, and in doing so, am experiencing my first foray into postgresql. Maybe I just never came across it in development, but I am unable to save entries into my database that are longer than 255 characters. Is there a way around this? Currently the I am just开发者_运维百科 using strings to store message data.

Secondly, if there is indeed a way to store more than 255 characters, is there a good way to convert my message strings into this form, using migrations? My app is currently live and in use.

Thanks!


use text instead of string on your migration type.


To change a migration

  1. script/generate migration change_string_to_text
  2. change_column :model, :attribute, :text
  3. rake db:migrate


Just to expand on @Codeglot's answer, :text is for (essentially) unlimited-length strings. :string with a limit:1234 option will limit the string to that length. On Postgres, a :string, limit:nil is effectively synonymous with :text. Although some databases store VARCHAR, VARCHAR(n), and TEXT types in different ways, leading to performance considerations, Postgres stores them all in the same way.

So if you want to exceed 255 chars but not be completely unbounded, you can use :string, limit:1234. Alternatively, you can use :text and restrict the length via validations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜