elixirdev

elixirdev

How to execute SQL SELECT query in migrations

I am using Triplex package for multi-tenants. when i create a tenant. its execute all migrations. after table creation when i execute seed migrations. it gives me error "{:error, "ERROR 42P01 (undefined_table) relation \"t2.tenants\" does not exist\n\n query: (SELECT MAX(id) from \"t2.tenants\")"} on select query. if remove select query, insert queries execute successfully.

defmodule TudoChat.Repo.Migrations.SeedTableTenants do
  use Ecto.Migration
  
  def up do
    execute("INSERT INTO t2.tenants (\"id\", \"inserted_at\", \"name\", \"status\", \"updated_at\") values ('1', '2019-07-08 10:37:28', 'tudo_', 'active', '2019-07-08 10:37:30')")
    execute("INSERT INTO t2.tenants (\"id\", \"inserted_at\", \"name\", \"status\", \"updated_at\") values ('2', '2019-07-08 10:38:19', 'test_', 'active', '2019-07-08 10:38:22')")
    execute("(SELECT MAX(id) from \"t2.tenants\")")
  end

  def down do
  end
end

please help me how to execute select query?

Marked As Solved

jola

jola

I’ll respond here too, on top of slack. Also I’m assuming postgres.

@kip is correct, "t2.tenants" is not the same as t2.tenants. However if you really want to quote it, you have to quote the schema and the table separately, "t2"."tenants".

You can test it easily in psql.

create table asd ();
select * from asd;
select * from "asd";
select * from public.asd;
select * from "public"."asd";
select * from "public.asd";
CREATE TABLE
SELECT 0
SELECT 0
SELECT 0
SELECT 0
ERROR:  relation "public.asd" does not exist
LINE 5: select * from "public.asd";

Also Liked

kip

kip

ex_cldr Core Team

I think the issue is that you are quoting t2.tenants in the SELECT statement. I think it should be:

execute("SELECT MAX(id) from t2.tenants")
hauleth

hauleth

Why do you wrap SELECT query in parens? Also I believe that @kip is right, and "t2.tenants" mean select table default."t2.tenants" where you wanted to select from t2.tenants which would be "t2"."tenants".

Where Next?

Popular in Questions Top

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
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
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

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
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 49522 488
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement