aadlani
aadlani

Reputation: 661

Insert or update a document in ArangoDB fails with constraint violated

I would like to achieve an Upsert in ArangoDB, but I can't get it working with the examples provided in the documentation of create-document. My requests fail with a unique constraint violated error message.

I'm inserting a document with a specific _key in a collection, and the overwrite option set to true as described in the documentation, but I still get the error messages. Here's the results of the described curl calls on my environment.

call #1:

curl -X POST --header 'accept: application/json'\
             --data-binary @- --dump - \
             http://localhost:8529/_api/document/products\?overwrite\=true \
<<EOF
{ "Hello": "Universe", "_key" : "lock" }
EOF

response #1:

HTTP/1.1 202 Accepted
X-Content-Type-Options: nosniff
Location: /_db/_system/_api/document/products/lock
Etag: "_X28xHz---_"
Server: ArangoDB
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
Content-Length: 58  

{"_id":"products/lock","_key":"lock","_rev":"_X28xHz---_"}

call #2: (identique to the first one)

curl -X POST --header 'accept: application/json'\
             --data-binary @- --dump - \
             http://localhost:8529/_api/document/products\?overwrite\=true \
<<EOF
{ "Hello": "Universe", "_key" : "lock" }
EOF

response #2:

HTTP/1.1 409 Conflict
X-Content-Type-Options: nosniff
Server: ArangoDB
Connection: Keep-Alive
Content-Type: application/json; charset=utf-8
Content-Length: 153

{"error":true,"errorMessage":"unique constraint violated - in index 0 of type primary over [\"_key\"]; conflicting key: lock","code":409,"errorNum":1210}

I went through the documentation, I enabled and reviewed the server logs but I can't figure it out. Any help would be highly appreciated.

Edit 1:

Just in case, if this can be of any help, I run ArangoDB server locally with Docker with the following docker-compose

version: '3'
services:
  db:
    image: arangodb
    ports:
    - "8529:8529"
    environment:
      ARANGO_NO_AUTH: 1
    volumes:
      - ./arangodb3:/var/lib/arangodb3
    command: "--log.level startup=trace --log.level requests=trace --log.level info"

And here's the log from the server:

db_1  | 2018-12-09T08:56:16Z [1] DEBUG {requests} "http-request-begin","0x7f51ac946010","172.20.0.1","POST","HTTP/1.1","/_api/document/products?overwrite=true"
db_1  | 2018-12-09T08:56:16Z [1] TRACE {requests} "http-request-body","0x7f51ac946010","{ \"Hello\": \"Universe\", \"_key\" : \"lock\" }\n"
db_1  | 2018-12-09T08:56:16Z [1] TRACE {requests} "http-request-response","0x7f51ac946010","/_api/document/products?overwrite=true","HTTP\/1.1 409 Conflict\r\nX-Content-Type-Options: nosniff\r\nServer: ArangoDB\r\nConnection: Keep-Alive\r\nContent-Type: application\/json; charset=utf-8\r\nContent-Length: 153\r\n\r\n{\"error\":true,\"errorMessage\":\"unique constraint violated - in index 0 of type primary over [\\\"_key\\\"]; conflicting key: lock\",\"code\":409,\"errorNum\":1210}"
db_1  | 2018-12-09T08:56:16Z [1] TRACE {requests} "http-request-statistics","0x7f51ac946010","172.20.0.1","POST","HTTP/1.1",409,41,153,"/_api/document/products?overwrite=true",read,0.000124931,queue,0.000000000,queue-size,0,request,0.000008106,total,0.017638445,error,false
db_1  | 2018-12-09T08:56:16Z [1] INFO {requests} "http-request-end","0x7f51ac946010","172.20.0.1","POST","HTTP/1.1",409,41,153,"/_api/document/products?overwrite=true",0.017630

Upvotes: 1

Views: 733

Answers (1)

aadlani
aadlani

Reputation: 661

I posted this question on the slack channel of the ArangoDB community, and the fine people there pointed to me that the replace-insert via the overwrite parameter was introduced only since in 3.4. Based on my docker compose, my image was pointing the the latest image of the Official Docker Repository for arangodb, which is 3.3.20.

I was explained that this "official" repository (arangodb) is not necessarily hosting the latest stable version of ArangoDB, and it can take days before it gets available, and I should instead point to arangodb/arangodb.

I adapted my docker-compose.yml file to use the right image, and now it works fine.

docker-compose.yml:

version: '3'
services:
  db:
    image: arangodb/arangodb:3.4.0
    ports:
    - "8529:8529"
    environment:
      ARANGO_NO_AUTH: 1
    volumes:
      - ./arangodb3:/var/lib/arangodb3

Thank you again to the ArangoDB Community on Slack, and more particularly to @mpoeter!

Upvotes: 3

Related Questions