tanweerdev
How to log ecto repo update query
I know ecto has Ecto.Adapters.SQL.to_sql(:all, repo, Post) which will give you raw sql for any query. But I want to inspect/debug an update which is giving me ** (Postgrex.Error) ERROR 21000 (cardinality_violation) more than one row returned by a subquery used as an expression . is there a way I can inspect changeset update inside test cases ie what query exactly was generated by ecto(why I wanna debug because inserting directly to postgres is working perfectly fine)
#Ecto.Changeset<
action: nil,
changes: %{
infection_rxs: [
#Ecto.Changeset<action: :replace, changes: %{}, errors: [],
data: #HaiData.InfectionRx<>, valid?: true>,
#Ecto.Changeset<
action: :insert,
changes: %{infection_id: 55, rx_id: 155},
errors: [],
data: #HaiData.InfectionRx<>,
valid?: true
>
]
},
errors: [],
data: #HaiData.Infection<>,
valid?: true
>
and by running Repo.update(changeset), I get
(Postgrex.Error) ERROR 21000 (cardinality_violation) more than one row returned by a subquery used as an expression
which is really strange for an update
Marked As Solved
blatyo
You can set the log level for test to debug and it’ll print to your console all the SQL executed in your tests. You can run one test, by doing mix test file:line, so that all you see is that one.
Also Liked
fuelen
I’d love an option to output raw SQL that can be copy/pasted directly into a Postgres GUI (like Postico). Makes for slick local debugging.
Recently I posted a library which allows interpolating arguments EctoLog - utility for inlining parameters into a query
mike_bianco
It looks like SQL logged isn’t immediately executable as a plain old SQL query:
SELECT TRUE FROM "places" AS p0 WHERE (p0."slug" = $1) LIMIT 1 ["the-slug"]: erl_level=debug ansi_color=cyan application=ecto_sql domain=elixir file=lib
You need to interpolate the array of arguments into the query in order to execute directly on the DB. I’d love an option to output raw SQL that can be copy/pasted directly into a Postgres GUI (like Postico). Makes for slick local debugging.
blatyo
I don’t disagree that it would be useful to have the values filled in to run. Just wanted to mention, that when Ecto sends the query, it sends it with the place holders in the query and the values separately. So, Ecto never actually builds a query that has the values in it.







