# Ecto insert with string keys only

**URL:** <https://forum.elixirforum.com/t/ecto-insert-with-string-keys-only/10530>\
**Category:** Questions\
**Tags:** ecto\
**Created:** [December 2, 2017, 9:34am UTC](https://forum.elixirforum.com/t/ecto-insert-with-string-keys-only/10530 "2017-12-02T09:34:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aeroith](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/a/b9bd4f/32.png) [@aeroith](https://forum.elixirforum.com/u/aeroith)\
**Post date:** [December 2, 2017, 9:34am UTC](https://forum.elixirforum.com/t/ecto-insert-with-string-keys-only/10530/1 "2017-12-02T09:34:23Z")

</div>

Hi, right now when I insert a JSON object into the db using ecto.insert() The primary keys are all atom. However when a value of a key is a map the keys of the map are all string. These things are causing inconsistencies and thus I want to store any kind of key as string. Any way to do that?

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [December 2, 2017, 9:35am UTC](https://forum.elixirforum.com/t/ecto-insert-with-string-keys-only/10530/2 "2017-12-02T09:35:06Z")

</div>

Can you provide an example?

How do you put and retrieve to/from database?  
How does your schema look like?  
etc.

---

<div class="post-metadata">

**Author:** ![aeroith](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/a/b9bd4f/32.png) [@aeroith](https://forum.elixirforum.com/u/aeroith)\
**Post date:** [December 2, 2017, 9:39am UTC](https://forum.elixirforum.com/t/ecto-insert-with-string-keys-only/10530/3 "2017-12-02T09:39:24Z")

</div>

```
@primary_key {:id, :binary_id, autogenerate: true}
  @derive {Phoenix.Param, key: :id}
  schema "dealers" do
    field :latitude, :float
    field :longitude, :float
    field :name, :map
  end

  def create_dealer(attrs \\ %{}) do
    %Dealer{id: Ecto.UUID.generate()}
    |> Dealer.changeset(attrs)
    |> Repo.insert()
  end

```

` def get_dealer!(id), do: Repo.get!(Dealer, id)`

```
%DealerFinder.Finder.Dealer{
  __meta__ : #Ecto.Schema.Metadata<:loaded, "dealers">,
  id: "08b8fbdd-5976-4da2-a1ce-715c07135c76",
  inserted_at: ~N[2017-12-01 14:35:26.646483],
  latitude: 51.53448,
  longitude: -0.11778,
  name: %{"en" => "Example dealer"},
  updated_at: ~N[2017-12-01 14:35:26.646493],
}

```

---

<div class="post-metadata">

**Author:** ![ryh](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ryh/32/13063_2.png) [@ryh](https://forum.elixirforum.com/u/ryh)\
**Post date:** [December 2, 2017, 10:07am UTC](https://forum.elixirforum.com/t/ecto-insert-with-string-keys-only/10530/4 "2017-12-02T10:07:58Z")

</div>

An embedded schema would allow you to have maps with atom keys, but there might be a lot of values with nil if not every field is supposed to have the same keys.

Another option is to create a custom type that converts the string keys back into atoms.

---

<div class="post-metadata">

**Author:** ![aeroith](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/a/b9bd4f/32.png) [@aeroith](https://forum.elixirforum.com/u/aeroith)\
**Post date:** [December 2, 2017, 10:23am UTC](https://forum.elixirforum.com/t/ecto-insert-with-string-keys-only/10530/5 "2017-12-02T10:23:45Z")

</div>

I actually want the quite opposite. Inserting to the db with string keys only. But I guess it’s not possible with Ecto

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [December 2, 2017, 10:31am UTC](https://forum.elixirforum.com/t/ecto-insert-with-string-keys-only/10530/6 "2017-12-02T10:31:36Z")

</div>

If your database supports JSON fields, it does store keys as strings, since this is the only keytype supported by JSON.

If though you want to have the keys of your scheme struct to be keys, then no you can’t do that at all with elixir. Structs do have atom keys and they have a fixed number of those.

---

<div class="post-metadata">

**Author:** ![aeroith](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/a/b9bd4f/32.png) [@aeroith](https://forum.elixirforum.com/u/aeroith)\
**Post date:** [December 2, 2017, 11:50am UTC](https://forum.elixirforum.com/t/ecto-insert-with-string-keys-only/10530/7 "2017-12-02T11:50:11Z")

</div>

Ok, thanks a lot for the help. Have a good day 🙂
