conflict="error" not being honoured with rethinkdbdash

50 Views Asked by At

I am using rethinkdbdash for the first time and I am attempting to simply create a user but error out if it exists. From all of the documentation I have read the following code should work, however it keeps inserting never actually detecting a conflict when I run it more than once. Am I doing this wrong in anyway?

r.table("users").insert({
    "username": "blahblah"
},
  conflict="error"
).run().then(function(response) {
  console.log('Success ', response);
})
.error(function(err) {
  console.log('ERROR occurred ', err);
})
1

There are 1 best solutions below

0
On BEST ANSWER

I have figured it out. I missed a very large part of the documentation. The conflict method looks at the primary key. In this case it is "id". "username" is a generic name.

Working code is:

r.table("users").insert({
    "id": "blahblah"
},
conflict = "error"
).run().then(function(response) {
console.log('SUCCESS: ', response);
}).error(function(err) {
console.log('ERROR: ', err);
});