How to convert following Ecto Query into the Plain RAW SQL
%Workspace{name: "#{prefix() || "public"}"} |> Repo.insert()
Your response will be highly appreicable.
How to convert following Ecto Query into the Plain RAW SQL
%Workspace{name: "#{prefix() || "public"}"} |> Repo.insert()
Your response will be highly appreicable.
regarding docs:
Ecto.Adapters.SQL.to_sql(:insert, Repo, %Workspace{name: "#{prefix() || "public"}"})
UPD sorry - :insert
is not supported (
MyApp.Repo.to_sql
works as well.
In Ecto 3.0 there are no to_sql
callback on repo
Did you try it?
It’s documented here: https://github.com/elixir-ecto/ecto_sql/blob/2ec71d74655cc076b6c965740a357dfe1505883a/lib/ecto/adapters/sql.ex#L228-L232 and here’s the macro, which adds it to the repo: https://github.com/elixir-ecto/ecto_sql/blob/2ec71d74655cc076b6c965740a357dfe1505883a/lib/ecto/adapters/sql.ex#L374-L427
It’s not part of the Ecto.Repo
behaviour though, which is why it’s not documented in that module. It’s only available if you’re using a SQL based adapter.
I want to query something like following
execute"""
insert into workspaces (
name
)
values("#{prefix() || "public"}");
"""
becuase I don’t want to use the schema in the command.
What are you trying to do exactly. Because I have strange feeling that we are dealing with XY problem.
It looks like you want to convert an ecto query to an sql query to use in migrations (hence the execute
). The problem here is that ecto actually cannot do that completely. Ecto as well as the underlying postgrex adapter does never interpolate parameters into the query. This is left to the database to do. So you’ll at best get the query with placeholders in the sql query and you’re left to replace those with actual values on your own. That’s why to_sql
returns a tuple of {query, parameters}
.