Migration error for a particular field

I have a migration where two particular fields are written like this

 add :labels, {:array, :text}
 add :tags, {:array, :text}

But in my schema it’s define like this

 field :labels, :string
 field :tags, :string

But when I change migration to string I’m getting this error

I’m getting data like this {one} so which type should I define here

(ArgumentError) cannot load [] as type :string for field :tags

So what should I do in this case?

If your database columns are Postgres arrays (assuming you use Postgres here), why do you declare them as strings in your schema instead of as arrays? Declaring them as strings won’t work because, as the error message says, Ecto cannot load a list (the array coming from the database) into a string (the field you declared).

The fields should be declared as arrays too, as your columns are arrays:

field :labels, {:array, :string}
field :tags, {:array, :string}
2 Likes

So you’re saying I should change in my schema right?

Correct. If you create the columns as arrays, the schema also needs to know that they are arrays, so it can present them as Elixir lists. In other words, your Ecto schema should reflect the actual database structure.

Thanks

1 Like