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
Hex Core Team
What is the type of the column in DB? Try setting :json or :jsonb in migration and it should work.
1
Popular in Questions
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
For example for a current url like
http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2,
I would like to get:
...
New
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
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
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
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
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
The documentation above suggests that while ...
New
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
I would like to know what is the best IDE for elixir development?
New
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
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
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
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
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
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
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
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








