如何调试:内部错误当前事务被中止,在事务b结束之前忽略命令

2024-06-15 04:50:03 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,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))

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

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

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

谢谢你的帮助和建议!


Tags: 模型truesqlmodelspostgresql选项错误exception
1条回答
网友
1楼 · 发布于 2024-06-15 04:50:03

在大多数情况下,这意味着前一条SQL语句未能执行。在这种情况下,您应该:

  1. 启用SQLlogging,请参阅下面要粘贴到settings.py中的代码片段

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

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

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

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. 将dir改为autocomplete_light_env/src/django-autocomplete-light/test_project

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

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

您的控制台将如下所示:

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

相关问题 更多 >