Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Types in Ecto - "value too long for type character varying(255)"

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

I understand (and assumed) that a string would be limited to a certain number of characters; however, I am not sure what type would be best for this scenario.

What type should I be using for a 'content' section of a blog in Phoenix framework?

The data will be paragraphs of text and cannot be limited in size.

Thanks in advance.

like image 403
Andrew Hendrie Avatar asked Mar 16 '17 00:03

Andrew Hendrie


1 Answers

The error you get is from the underlying database where the column type is set to varchar which is what's created by default when you specify the column type as string in a migration.

To store a variable length string over 255 characters, you need to specify the column type as text in the migration. You can convert the type of the existing column to text by using a migration such as:

alter table(:posts) do
  modify :content, :text
end

The field type in the schema section of the model should remain as string:

schema "posts" do
  field :content, :string
end
like image 156
Navin Peiris Avatar answered Oct 10 '22 23:10

Navin Peiris