如何调试:内部错误当前事务被中止,命令被忽略直到事务块结束

时间:2012-01-30 12:41:31

标签: python django postgresql postgis geodjango

嗨Stackoverflow人,

我使用GeoDjango完成了我的第一步,我正在寻找更好的选项来检查错误的sql语句。

到目前为止,我只是想在postgresql表中保护lng + lat点。

模型定义为:

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

    objects = models.GeoManager()

在我看来,我尝试执行以下命令

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

但是当我尝试保存表单时收到以下错误:

  

异常类型:InternalError   例外值:当前交易是   中止,命令被忽略,直到事务块结束

出现此错误的原因是什么?我相信sql语句可能有问题,但检查的最佳方法是什么? Django只提供一般错误消息“内部错误”。

感谢您的帮助和建议!

1 个答案:

答案 0 :(得分:27)

在大多数情况下,这意味着之前的 SQL语句无法执行。在这种情况下,你应该:

  1. 启用SQL logging ,请参阅以下代码段以粘贴settings.py

  2. 设置DEBUG = 1 ,否则将不会记录SQL

  3. 再次运行runserver ,您应该会在控制台中看到所有SQL查询

  4. 直接在数据库中执行最后的SQL查询,然后您应该找到哪些查询失败然后您应该能够调试它们 - 或者打开一个特定于导致问题的查询。您可以使用phpMyAdmin,或直接使用CLI客户端或任何数据库客户端逐个执行SQL查询,直到找到需要爱的那个查询。

  5. SQL日志记录配置:

    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',
           },  
       }   
    }
    

    如果此配置未提供runserver的任何其他控制台输出,请随时尝试django-autocomplete-light's example test_project

    1. /tmp

    2. 中读取并粘贴安装命令
    3. 将目录更改为autocomplete_light_env/src/django-autocomplete-light/test_project

    4. 打开test_project/settings.py,将LOGGING配置替换为上面的

    5. 运行服务器并打开浏览器

    6. 您的控制台将如下所示:

      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=[]