Search code examples
postgresqlphoenix-frameworkecto

Postgres error running seeds.exs (using Ecto) - `string_data_right_truncation`


enter code hereI've got a Postgrex error when running my seeds.exs file with MIX: ** (Postgrex.Error) ERROR (string_data_right_truncation): value too long for type character varying(255)

I googled it and the solution seems to be to use Postgresql type text instead of string. But I think Ecto doesn't have that type. So, what's the real problem?

The error debug message in the console is:

[debug] QUERY ERROR db=7.9ms
INSERT INTO "eventos" ("date","imgPath","text","title","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5,$6) RETURNING "id" ["12 MAR 2016", "images/specific/eventos/sixtyfour/1.jpg", "Aberta há mais de um ano, a Royal City Studios fez a sua primeira aparição pública com um concerto dos vimaranenses The Wild Booze este \n\tSábado passado, dia 12 de Março.</br>A Royal City Studios é um estúdio com 300 metros quadrados de área total que promete albergar projectos \n\tmusicais dos mais variados géneros. Equipado de uma régie recheada de bom material, sala de ensaios, 'quarto seco' e ainda de um enorme estúdio \n\tde música com capacidade para albergar uma orquestra.</br>Localizado na Fábrica ASA, este estúdio tem ideias de realizar live sessions (à lá \n\tBlogoteque ou KEXP) com banda a ser filmada num ambiente natural e intimista.", "OPEN DAY ROYAL CITY STUDIOS", {{2016, 9, 21}, {11, 7, 19, 0}}, {{2016, 9, 21}, {11, 7, 19, 0}}]

Solution

  • You can use the :text type which is not length restricted.

    Here are the relevant sections from the docs for Ecto.Migration.add/3

    However, the column type is not always the same as the type used in your schema. For example, a schema that has a :string field, can be supported by columns of types :char, :varchar, :text and others. For this reason, this function also accepts :text and other columns, which are sent as is to the underlying database.

    ...

    options

    • :size - the size of the type (for example the numbers of characters). Default is no size, except for :string that defaults to 255.