abhay
March 30, 2018, 11:46am
1
While working with ecto schema connecting to Mysql as database I tried using list data type. I used {:array, :inner_type} for storing list into the database as per hex docs info https://hexdocs.pm/ecto/Ecto.Schema.html .but it shows an error that MySQL doesn’t support array, so is there any solution?
Mysql doesn’t support list, that documentation is for postgresql I think. You can create an ecto custom type which will encode array as json and then save as string to mysql.
opened 03:43AM - 28 Jul 16 UTC
closed 08:27AM - 28 Jul 16 UTC
How can I use store list in mysql?
### Precheck
I find an old issue about this … https://github.com/drewolson/scrivener/issues/9.
but I don't know the `count(1)` mean.
### Environment
- Elixir version (elixir -v): Erlang/OTP 18 [erts-7.3] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
Elixir 1.2.4
- Database and version (PostgreSQL 9.4, MongoDB 3.2, etc.):
mysql Ver 14.14 Distrib 5.5.44, for debian-linux-gnu (x86_64) using readline 6.3
- Ecto version (mix deps):
```
defp deps do
[{:phoenix, "~> 1.2.0"},
{:phoenix_pubsub, "~> 1.0"},
{:mariaex, ">= 0.0.0"},
{:phoenix_ecto, "~> 3.0-rc"},
{:phoenix_html, "~> 2.4"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.9"},
{:comeonin, "~> 2.4"},
{:cowboy, "~> 1.0"},
{:exrm, "~> 1.0"}]
end
```
- Database adapter and version (mix deps):
Above
- Operating system:
ubuntu 14.04
### Current behavior
In migration
```
def change do
create table(:user, primary_key: false) do
add :id, :integer, primary_key: true
add :name, :string
add :lvl, :integer
add :games, {:array, :integer}
```
In model
```
@primary_key {:id, :id, autogenerate: false}
schema "user" do
field :name, :string
field :lvl, :integer
field :games, {:array, :integer}
field :status, :integer
```
when I run `mix ecto.migrate` it comes an error
```
11:06:20.575 [info] create table user
** (ArgumentError) Array type is not supported by MySQL
(ecto) lib/ecto/adapters/mysql/connection.ex:753: Ecto.Adapters.MySQL.Connection.error!/2
(ecto) lib/ecto/adapters/mysql/connection.ex:652: Ecto.Adapters.MySQL.Connection.column_type/2
(ecto) lib/ecto/adapters/mysql/connection.ex:584: Ecto.Adapters.MySQL.Connection.column_definition/2
(elixir) lib/enum.ex:1120: anonymous fn/4 in Enum.map_join/3
(elixir) lib/enum.ex:1473: Enum."-reduce/3-lists^foldl/2-0-"/3
(elixir) lib/enum.ex:1118: Enum.map_join/3
(ecto) lib/ecto/adapters/mysql/connection.ex:498: Ecto.Adapters.MySQL.Connection.execute_ddl/1
(ecto) lib/ecto/adapters/mysql.ex:99: Ecto.Adapters.MySQL.execute_ddl/3
(ecto) lib/ecto/migration/runner.ex:101: anonymous fn/2 in Ecto.Migration.Runner.flush/0
(elixir) lib/enum.ex:1473: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto) lib/ecto/migration/runner.ex:99: Ecto.Migration.Runner.flush/0
(stdlib) timer.erl:181: :timer.tc/2
(ecto) lib/ecto/migration/runner.ex:27: Ecto.Migration.Runner.run/6
(ecto) lib/ecto/migrator.ex:121: Ecto.Migrator.attempt/6
(ecto) lib/ecto/migrator.ex:71: anonymous fn/4 in Ecto.Migrator.do_up/4
(ecto) lib/ecto/adapters/sql.ex:472: anonymous fn/3 in Ecto.Adapters.SQL.do_transaction/3
(db_connection) lib/db_connection.ex:973: DBConnection.transaction_run/4
(db_connection) lib/db_connection.ex:897: DBConnection.run_begin/3
(db_connection) lib/db_connection.ex:671: DBConnection.transaction/3
(ecto) lib/ecto/migrator.ex:244: anonymous fn/4 in Ecto.Migrator.migrate/4
```
Some of ecto features are only supported by some adapters - MySQL in general does not support more advanced features, arrays are one of them (this is not a limitation of Ecto, but of MySQL). There are several options of emulating them - either serialising to JSON or simple comma separated string, or even having a separate table.
2 Likes
dsnipe
March 30, 2018, 2:27pm
4
I had the same problem.
Decided to use an old plain one-to-many association instead. it’s a SQL database in the end.
1 Like
abhay
March 31, 2018, 4:12am
5
Thanks for your replies it is very helpful
c4710n
September 25, 2021, 7:50am
7
As a supplement at 2021…
Ecto v3 with MySQL < 5.7.8 (don’t support JSON) :
use :text
in migrations.
use :map
or {:array, inner_type}
at schema level, and Ecto will encode and decode it for you.
Ecto v3 with MySQL >= 5.7.8:
use :map
or :json
in migrations.
use :map
or {:array, inner_type}
at schema level.
3 Likes
I am using the :map
in migration and in schema field :filed_name, {:array, :integer}
But when i query the table I am getting the below error
cannot load
“[1,3,23]” as type {:array, :integer} for field :filed_name
is there anything i am doing wrong?
c4710n
April 10, 2022, 7:12am
9
What is the version of MySQL that you are using?