donglingyi4679 2019-09-08 22:42
浏览 164

如何获取unique_constraint违例的列名?

I am using the pq driver and I'm wondering why the pq.Error gives an empty Column when I face a unique constraint violation.

I could parse Detail but is there any reason why Column would be empty? It would be preferable if I could just get email from Column instead of parsing Detail

Here is what the error looks like:

Severity:"ERROR"
Code:"23505"
Message:"duplicate key value violates unique constraint "unique_users""
Detail:"Key (email)=(user3@email.com) already exists."
Hint:""
Position:""
InternalPosition:""
InternalQuery:""
Where:""
Schema:"public"
Table:"users"
Column:""
DataTypeName:""
Constraint:"unique_users"
File:"nbtinsert.c"
Line:"534"
Routine:"_bt_check_unique"

Unfortunately, the Column value is empty. I'm trying to come up with error messaging for my application and I'm wondering if there is some way to get that information so I can enhance the message why the entity was not created and let the caller know the field (email in this case) as well.

  • 写回答

1条回答 默认 最新

  • dongmieqiao3152 2019-09-08 22:45
    关注

    The specific error message (including the violated constraint and column/value) is in the field Detail.

    EDIT:

    I guess Column is empty, because the unique constraint might affect multiple columns. Constraint offers the constraint name, in combination with Schema and Table you can look up this constraint in pg_constraint. Field conkey might hold the references to the covered attributes.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?