Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug: Internal Error current transaction is aborted, commands ignored until end of transaction block

Hi Stackoverflow people,

I do my first steps with GeoDjango and I am looking for better options to check faulty sql statements.

So far, I simply wanted to safe a lng+lat point in my postgresql table.

The model is defined with:

    geolocation = models.PointField(_('Geo Location'), 
                geography=True, 
                null = True, 
                blank = True,
                help_text=_('Geolocation with Longitude and Latitude'))

    objects = models.GeoManager()

In my view, I try to execute the following command

savedProject.geolocation = GEOSGeometry('POINT(%s %s)' %(u_lng,u_lat))

but I receive the following error when I try to save the form:

Exception Type: InternalError Exception Value: current transaction is aborted, commands ignored until end of transaction block

What is the reason for this error? I believe that there could be something wrong with the sql statement, but what is the best way to check? Django just provides with general error message "Internal Error".

Thank you for your help and suggestions!

like image 706
neurix Avatar asked Jan 30 '12 12:01

neurix


People also ask

What causes the current transaction to abort?

Explanation: This log event happens when a transaction fails due to a potentially unrelated error, and you try to run another query in the failed transaction.

Which syntax is used to abort the current transaction in postgresql?

Use COMMIT to successfully terminate a transaction. Issuing ABORT outside of a transaction block emits a warning and otherwise has no effect.

How do I rollback a transaction in postgresql?

To roll back a prepared transaction, you must be either the same user that executed the transaction originally, or a superuser. But you do not have to be in the same session that executed the transaction. This command cannot be executed inside a transaction block. The prepared transaction is rolled back immediately.


1 Answers

In most cases this means that the previous SQL statement failed to execute. In this case you should:

  1. Enable SQL logging, see the following snippet to paste in settings.py

  2. Set DEBUG=1, or SQL won't be logged

  3. Run runserver again, and you should see all SQL queries in the console

  4. Execute the last SQL queries directly in your database, you should then find which queries fail and then you should be able to debug them - or open a new question which is specific to the query that causes the problem. You can use phpMyAdmin, or directly a CLI client, or whatever database client, to execute the SQL queries one by one until you find the one that needs some love.

SQL Logging configuration:

LOGGING = { 
   'version': 1,
   'disable_existing_loggers': True,
   'formatters': {
       'simple': {
           'format': '%(levelname)s %(message)s',
       },  
   },  
   'handlers': {
       'console':{
           'level':'DEBUG',
           'class':'logging.StreamHandler',
           'formatter': 'simple'
       },  
   },  
   'loggers': {
       'django': {
           'handlers': ['console'],
           'level': 'DEBUG',
       },  
   }   
}

If this configuration does not provide any additional console output with runserver, then feel free to try django-autocomplete-light's example test_project:

  1. Read and paste the installation commands in /tmp

  2. Change dir to autocomplete_light_env/src/django-autocomplete-light/test_project

  3. Open test_project/settings.py, replace the LOGGING configuration by the one above

  4. Runserver and open your browser

Your console will look like:

Validating models...

0 errors found
Django version 1.4.1, using settings 'test_project.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
DEBUG (0.001) SELECT "django_content_type"."id", "django_content_type"."name", "django_content_type"."app_label", "django_content_type"."model" FROM "django_content_type" WHERE ("django_content_type"."model" = taggable  AND "django_content_type"."app_label" = charfield_autocomplete ); args=('taggable', 'charfield_autocomplete')
DEBUG (0.000) 
        SELECT DISTINCT "tagging_tag".id, "tagging_tag".name
        FROM
            "tagging_tag"
            INNER JOIN "tagging_taggeditem"
                ON "tagging_tag".id = "tagging_taggeditem".tag_id
            INNER JOIN "charfield_autocomplete_taggable"
                ON "tagging_taggeditem".object_id = "charfield_autocomplete_taggable"."id"

        WHERE "tagging_taggeditem".content_type_id = 11

        GROUP BY "tagging_tag".id, "tagging_tag".name

        ORDER BY "tagging_tag".name ASC; args=[]
like image 178
jpic Avatar answered Sep 19 '22 08:09

jpic