tanweerdev

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

Showing Posts 1 to 10

blatyo

blatyo

Conduit Core Team

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

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

blatyo

Conduit Core Team

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

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

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

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

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

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 :slight_smile:

fuelen

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

mike_bianco

Neat, that’s exactly what I’m looking for! Thank you.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
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
spammy
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
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
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
bottlenecked
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
rahultumpala
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 Top

JesseHerrick
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews