# Methods of storing arrays in MySQL

**URL:** <https://discourse.rom-rb.org/t/methods-of-storing-arrays-in-mysql/305>\
**Category:** Issues\
**Created:** [February 22, 2019, 8:46pm UTC](https://discourse.rom-rb.org/t/methods-of-storing-arrays-in-mysql/305 "2019-02-22T20:46:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ethnt](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/ethnt/32/167_2.png) [@ethnt](https://discourse.rom-rb.org/u/ethnt)\
**Post date:** [February 22, 2019, 8:46pm UTC](https://discourse.rom-rb.org/t/methods-of-storing-arrays-in-mysql/305/1 "2019-02-22T20:46:19Z")

</div>

Hi all,

I’m converting a project that currently uses Mongoid to use ROM and MySQL. Due to some infrastructure restrictions, we need to use MySQL, rather than PostgreSQL or another RDBMS.

I need to be able to store an array of strings. This would be fairly easy to do with Postgres, but not so with MySQL. From what I understand, I can do something like this to set write/read types:

```
module Persistence
  module Relations
    class Countries < ROM::Relation[:sql]
      JSONWrite = Dry::Types::Constructor.new(Dry::Types['string'], fn: ->(v) { JSON.dump(v) })
      JSONRead = Dry::Types::Constructor.new(Dry::Types['string'], fn: ->(v) { JSON.parse(v) })

      schema(:countries) do
        attribute :id, Types::Integer
        attribute :number_types, ROM::Types::String, read: JSONRead

        primary_key :id
      end
    end
  end
end

```

However, when I try to then create a record:

```
Dry::Types::SchemaError: ["geographic", "toll_free"] (Array) has invalid type for :number_types violates constraints (no implicit conversion of Array into String failed)
from /Users/ethan/.asdf/installs/ruby/2.5.3/lib/ruby/gems/2.5.0/gems/dry-types-0.14.0/lib/dry/types/hash/schema.rb:192:in `rescue in block in coerce'
Caused by TypeError: no implicit conversion of Array into String
from /Users/ethan/.asdf/installs/ruby/2.5.3/lib/ruby/2.5.0/json/common.rb:156:in `initialize'

```

The column `number_types` is set of `TEXT` MySQL type, so I’m not sure where to go from here.

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)\
**Post date:** [February 23, 2019, 8:48pm UTC](https://discourse.rom-rb.org/t/methods-of-storing-arrays-in-mysql/305/2 "2019-02-23T20:48:18Z")

</div>

You configured db type to be `Types::String`, you want to set `JSONWrite` there instead. Otherwise rom assumes the input will be a string, and you pass an array, so it must go through your `JSONWrite`.

---

<div class="post-metadata">

**Author:** ![ethnt](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/ethnt/32/167_2.png) [@ethnt](https://discourse.rom-rb.org/u/ethnt)\
**Post date:** [February 27, 2019, 9:34pm UTC](https://discourse.rom-rb.org/t/methods-of-storing-arrays-in-mysql/305/3 "2019-02-27T21:34:43Z")

</div>

Thanks for the help! Looks like it’s saving correctly, however it’s not reading correctly. From throwing a `binding.pry` into the `JSONRead` type, it’s actually reading it twice: once when it’s actually a string, and then again when it’s an array. Why would it be doing this?

I guess a better question to ask is if there’s a suggested way to do serialization with ROM?

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)\
**Post date:** [March 1, 2019, 10:26am UTC](https://discourse.rom-rb.org/t/methods-of-storing-arrays-in-mysql/305/4 "2019-03-01T10:26:02Z")

</div>

Oh this doesn’t sound good. Are you able to reproduce this in a simple standalone script?

---

<div class="post-metadata">

**Author:** ![ethnt](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/ethnt/32/167_2.png) [@ethnt](https://discourse.rom-rb.org/u/ethnt)\
**Post date:** [March 1, 2019, 2:12pm UTC](https://discourse.rom-rb.org/t/methods-of-storing-arrays-in-mysql/305/5 "2019-03-01T14:12:13Z")

</div>

I’m not sure I’m able to reproduce this in a standalone script — I realized we’re using the edge version of ROM from the GitHub repo, downgrading fixed the issue.

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.rom-rb.org/solnic/32/279_2.png) [@solnic](https://discourse.rom-rb.org/u/solnic)\
**Post date:** [March 7, 2019, 3:41pm UTC](https://discourse.rom-rb.org/t/methods-of-storing-arrays-in-mysql/305/6 "2019-03-07T15:41:33Z")

</div>

oh that’s very useful info, we’ll look into it.
