Ecto 中的数据类型 - "value too long for type character varying(255)"

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)

我理解(并假设)一个字符串将被限制为一定数量的字符;但是,我不确定哪种类型最适合这种情况。

我应该为 Phoenix 框架中博客的 'content' 部分使用什么类型?

数据将是文本段落,大小不受限制。

提前致谢。

您收到的错误来自基础数据库,其中列类型设置为 varchar,这是在迁移中将列类型指定为 string 时默认创建的内容。

要存储超过 255 个字符的可变长度字符串,您需要在迁移中将列类型指定为 text。您可以使用以下迁移将现有列的类型转换为 text

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

模型的架构部分中的字段类型应保持为 string:

schema "posts" do
  field :content, :string
end