Ecto query into Plain SQL

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: and here’s the macro, which adds it to the repo:

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

    insert into workspaces (
      values("#{prefix() || "public"}");

becuase I don’t want to use the schema in the command.

In this case use query

What are you trying to do exactly. Because I have strange feeling that we are dealing with XY problem.

1 Like

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}.