简体   繁体   English

确定哪个唯一约束导致ArangoDB中的INSERT错误

[英]Determining which unique constraint caused INSERT failure in ArangoDB

I have a document collection in ArangoDB that has multiple unique indexes. 我在ArangoDB中有一个文档集合,该文档集合具有多个唯一索引。 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 messages are non-specific: 错误消息是非特定的:

{ 
    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. 谢谢。

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: 今天处理此问题的唯一方法是使用FILTER对您尝试插入的值进行后续选择:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM