AlchemistCamp

AlchemistCamp

Ecto Migration to add JSONB index

How do you create an index on a JSONB field in an Ecto migration? I’m trying to do the equivalent of this:

CREATE TABLE comments(id INT, body TEXT, data JSONB); 
CREATE INDEX ON comments((meta->'author'->>'id'));

What should I fill into the ??? below?

def change do
  create table(:comments) do
    add :meta, map
    add :body, text
    #...
    timestamps()
  end

  create index(???, ???)
end

Note, that the meta is coming in is user generated, so I’m not 100% sure what the structure will be to begin with and will likely want to add more indexes as time goes by.

Marked As Solved

cblavier

cblavier

If I were you, I would create the index with a raw SQL query. As in following PostGIS index creation

defmodule MyApp.AddPoiPostgisIndices do
  use Ecto.Migration

  def up do
    execute("CREATE INDEX pois_coordinates_index ON pois USING GIST(coordinates);")
  end

  def down do
    execute("DROP INDEX pois_coordinates_index")
  end
end

Also Liked

AlchemistCamp

AlchemistCamp

Looks straightforward enough. I’ll just drop my query into the execute like so:
execute("CREATE INDEX ON comments((meta->'author'->>'id'));")

Thanks!

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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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 52673 488
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

We're in Beta

About us Mission Statement