johnnyicon
How do I use the Postgres JSONB / Postgrex JSON extension?
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres’ JSONB via the :map datatype.
Here’s my migration to add it to my table:
defmodule MyApp.Repo.Migrations.AddMapFieldToUser do
use Ecto.Migration
def change do
alter table(:users) do
add :data, :map
end
end
end
Here’s my schema:
defmodule MyApp.User do
use MyApp.Web, :model
use Coherence.Schema
schema "users" do
field :first_name, :string
field :last_name, :string
field :email, :string
field :data, :map
coherence_schema()
timestamps()
end
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:first_name, :last_name, :email] ++ coherence_fields)
|> unique_constraint(:email)
|> validate_required([:first_name, :last_name, :email])
|> validate_coherence(params)
end
end
I’ve been trying to set values into the data map using a changeset, but I haven’t had any luck. I’ve tried using both a JSON string and a map.
JSON string:
changeset = User.changeset(retrieved_user, %{data: "{test:'awesome'}"})
#Ecto.Changeset<action: nil, changes: %{}, errors: [],
data: #MyApp.User<>, valid?: true>
Map:
changeset = User.changeset(john, %{data: %{test: "awesome"}})
#Ecto.Changeset<action: nil, changes: %{}, errors: [],
data: #PodioBackup.User<>, valid?: true>
You’ll see that the changes map is empty.
Which leads me to believe that I am clearly missing something!
And I’m thinking maybe I need to add this JSON extension that many websites are referring to: postgrex/lib/postgrex/extensions/json.ex at master · elixir-ecto/postgrex · GitHub
How do I actually use that JSON extension? And, what am I doing wrong?
Thanks for the help in advance! ![]()
Marked As Solved
alexgaribay
You’re missing a cast of :data in your changeset.
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:first_name, :last_name, :data] ++ coherence_fields)
|> ...
end
Also Liked
michalmuskala
They are equivalent when using postgres. map is an abstract type and each database adapter can choose the actual representation - for postgres it’s jsonb.
Qqwy
So is a correct conclusion that using :map would be preferable over using :json, to allow you to switch database adapters, if desired, later on?
Popular in Questions
Other popular topics
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








