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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews