Wouldn't it be nice for Ecto if PostgreSQL listed all violated constraints at once?

Ecto has nice support for contraints, which I’ve written about and contributed to.

But when you violate multiple constraints, PostgreSQL only tells you about them one at a time. This means that a user would have to fill out a form, submit, fix an error, resubmit, fix another error… etc. To avoid this, we have to check things like NOT NULL with validations in addition to the underlying database constraint.

If PostgreSQL could tell us all the violated constraints at once, that would simplify things, wouldn’t it?

If you agree, maybe go upvote this item on the PostgreSQL user voice? https://postgresql.uservoice.com/forums/21853-general/suggestions/308845-give-names-of-violated-constraint

4 Likes

So, that PostgreSQL issue was closed with the following message:

This was implemented in 9.3. See PG_DIAG_CONSTRAINT_NAME.

I found that on this page, but:

  • it’s not clear to me if using PQresultErrorField with PG_DIAG_CONSTRAINT_NAME would return all violated constraints or just the first one, like I see in psql
  • I know beans about programming C, so I don’t know how to test this. I don’t really even know whether or where libpq figures into the Ecto stack.

My understanding is that we have something like this:

    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚       Ecto        β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              β”‚  
              β”‚   uses
              β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚     Postgrex      β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
              β”‚  connects on 
              β”‚  port 5432 (or other)
              β–Ό
    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
    β”‚ PostgreSQL Server β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Is libpq involved here somewhere? If so, is there any way to try to get it to query this information?

@ericmj I’d love to help us make use of this, but I need to get a clue about the stack first. :slight_smile:

1 Like

Postgrex does not use libpq - the PostgreSQL protocol is implemented natively in Elixir.

1 Like

libpq and postgrex follow the same constraints since they speak the same protocol, the only difference is that libpq uses text serialisition and postgrex uses the binary format, but that should not matter in this case.

Unfortunately I don’t think the issue you linked solves what you want. Ecto already uses the feature in the issue to link errors with changeset validations. The issue doesn’t seem to have a solution for multiple errors reported from Postgres.

1 Like