tanweerdev
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
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
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.mike_bianco
It looks like SQL logged isn’t immediately executable as a plain old SQL query:
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.
LostKobrakai
I seem to recall recently seeing a project recently, which would do such interpolation for logs, but can’t find it anymore. But as @blatyo mentioned, ecto doesn’t interpolate into the query, it doesn’t even have code for doing that in the first place. Everything is sent separately to the db.
mike_bianco
Makes sense! I imagine some library that Ecto depends on ends up rendering the raw SQL, right? Is there a way to configure that library to output raw rendered SQL?
gregvaughn
I have not dug into the code to verify, but I believe this format is ultimately how the low level driver communicates with Postgres. This allows Postgres to create prepared statements where it has analyzed and determined a query plan and cached that so that future calls of the same SQL “template” are faster.
This blog is tangentially relevant to understanding a particular case where the prepared statements were causing negative performance characteristics in a rare situation. Elixir and Postgres: A Rarely Mentioned Problem | Lainblog
LostKobrakai
That’s not the case. The data is never interpolated. Query and parameters are sent to postgres as separate things. This is for performance reasons (preparing statements), but also for security reasons. When you don’t interpolate, then there’s no attack vector for sql injection attacks.
So the only thing possibly being able to know the query with inlined values would be postgres itself, but I’d even doubt the db will “merge” the query just to parse things out again.
mike_bianco
Oh, interesting! I didn’t know that PG allows you to send parameterized queries along. That’s really neat, very much makes sense why we can’t expose the raw SQL then
fuelen
Recently I posted a library which allows interpolating arguments EctoLog - utility for inlining parameters into a query
mike_bianco
Neat, that’s exactly what I’m looking for! Thank you.