Determining which unique constraint caused INSERT failure in ArangoDB

三世轮回 提交于 2019-12-25 08:33:35

问题


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 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.


回答1:


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


来源:https://stackoverflow.com/questions/41819878/determining-which-unique-constraint-caused-insert-failure-in-arangodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!