确定哪个唯一约束导致ArangoDB中的INSERT失败 [英] Determining which unique constraint caused INSERT failure in ArangoDB

查看:139
本文介绍了确定哪个唯一约束导致ArangoDB中的INSERT失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ArangoDB中有一个文档集合,该文档集合具有多个唯一索引.插入新文档时,由于任何唯一索引,插入可能会失败.有没有一种方法可以轻松找出文档中的哪个字段导致插入失败?

I have a document collection in ArangoDB that has multiple unique indexes. When inserting a new document, the insert could fail because of any of the unique indexes. Is there a way to easily figure out which field(s) in the document caused the insert to fail?

例如,采用一个存储用户数据的集合. 用户名"和电子邮件"字段上的唯一索引意味着,如果这些字段中的任何一个重复,则插入操作可能会失败.

For example, take a collection that stores user data. Unique indexes on both the "username" and "email" fields mean that an insert could fail if either of those fields are duplicated.

错误消息不是特定的:

{ 
    error: true,
    errorMessage: 'unique constraint violated (while executing)',
    code: 409,
    errorNum: 1210
}

很长的路要走,就是分别输入/更新这些唯一字段,以便确定哪个字段违反了唯一约束.或者尝试检索与我们的输入值匹配的文档,以在尝试插入之前确定是否会发生冲突.我只是觉得必须有一种更简单的方法.

The long way around would be to input/update these unique fields separately to know for certain which field violated the unique constraint. Or try retrieving documents that match our input values to identify if there would be a collision before trying to insert. I just have a feeling that there must be a simpler way.

有没有一种方法可以将字段名和错误一起返回?还是我从完全错误的角度来解决问题?

Is there a way to return the field name along with the error? Or am I approaching the problem from entirely the wrong angle?

真的很感谢您的任何想法或建议.谢谢.

Would really appreciate any thoughts or suggestions. Thanks.

推荐答案

对不起,目前尚没有实现此目标的聪明方法. 今天处理此问题的唯一方法是对您尝试插入的值进行FILTER的后续选择:

I'm sorry, there is currently no smart way to achieve this. The only way to handle this today, is to do a subsequent select with a FILTER to the values you tried to insert:

FOR doc IN collection
  FILTER doc.firstindexed = 'firstvalue'
      OR doc.secondindexed = 'secondvalue'
    RETURN doc

这篇关于确定哪个唯一约束导致ArangoDB中的INSERT失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆