callmeahab

callmeahab

How to insert array of maps in Ecto

I have a model with field of type array defined:

defmodule QsiSearchService.Features.FeatureProperties do
  use Ecto.Schema
  import Ecto.Changeset
  alias QsiSearchService.Features.{FeatureProperties, FeatureType}

  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id
  schema "feature_properties" do
    field :fields, {:array, :map}
    belongs_to :feature_type, FeatureType

    timestamps()
  end

  @doc false
  def changeset(%FeatureProperties{} = feature_properties, attrs) do
    feature_properties
    |> cast(attrs, [:fields, :feature_type_id])
    |> validate_required([:fields, :feature_type_id])
  end
end

When I try creating a record:

@feature_properties_attrs %{fields: [
    %{"name" => "PROGRESS_STATUS", "type" => "string"},
    %{"name" => "VOLTAGE", "type" => "number"}
  ]}

{:ok, feature_properties} = 
      @feature_properties_attrs
      |> Map.put(:feature_type_id, created_feature_type.id)
      |> Features.create_feature_properties()

I get the following error:

 ** (ArgumentError) Postgrex expected a binary, got %{"name" => "PROGRESS_STATUS", "type" => "string"}. Please make sure the value you are passing matches the definition in
 your table or in your query or convert the value accordingly.

Is this kind of operation possible, or do I have to create this schema using embeds?

Thanks

Most Liked

wojtekmach

wojtekmach

Hex Core Team

What is the type of the column in DB? Try setting :json or :jsonb in migration and it should work.

Where Next?

Popular in Questions Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New

We're in Beta

About us Mission Statement