0

This is a follow up question to this:

Postgres: More verbose error message: I am missing the table name

PostgreSQL 9.6 improved error messages with version 9.6.

With psql you can enable it \set VERBOSITY verbose.

How to enable this for every connection inside django ORM?

Background: I want better error messages.

Example: I am missing the table name in messages like this

IntegrityError: null value in column "date" violates not-null constraint
DETAIL:  Failing row contains (10005, null, f, TEST, MAIL).

I think the relevant part of the 9.6 release notes is this:

Add support in libpq for regenerating an error message with a different verbosity level (Alex Shulgin)

This is done with the new function PQresultVerboseErrorMessage(). This supports psql's new \errverbose feature, and may be useful for other clients as well.

I use psycopg2 as database adapter.

4
  • you are working with psycopg2 between django and postgresql. Thus those features should be transcluded by psycopg2 first. As I can see from docs - psycopg2 doesn't provides this neither in main code base nor in extras. :( Commented Feb 7, 2017 at 8:24
  • for server-side features you can always right after starting the transaction execute SET client_min_messages DEBUG5; Commented Feb 7, 2017 at 8:40
  • @IlyaDyoshin I guess "\set VERBOSITY verbose" and "client_min_messages DEBUG5;" are different, or am I wrong? Commented Feb 7, 2017 at 11:39
  • yes those are not different. Thus \set VERBOSITY verbose is pure libpq and psql functionality, which have to be reflected in wrappers like psycopg2 Commented Feb 7, 2017 at 11:54

1 Answer 1

4
+50

Try this

# settings.py
DATABASES = {
    'default': {
        'ENGINE': '...',
        'OPTIONS': {
            'init_command': "SET log_error_verbosity TO 'verbose'",
        },
    }
}

Test query

SELECT setting FROM pg_settings WHERE name = 'log_error_verbosity'
Sign up to request clarification or add additional context in comments.

4 Comments

this is for mysql but how can i do the same for prostgres?
This is for PostgreSQL postgresql.org/docs/12/…
i mean, Django has init_command only when it uses MySQL, init_command is not avaible for postgres docs.djangoproject.com/en/3.0/ref/databases/… i got this error: psycopg2.ProgrammingError: invalid dsn: invalid connection option "init_command" because that command is not for postgres
Seems this is not possible via configuration options. You can try to create django.db.backends.signals. Example gist.github.com/ketanbhatt/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.