javascriptnode.jsrethinkdbrethinkdb-javascriptrethinkdbdash

conflict="error" not being honoured with rethinkdbdash


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);
})

Solution

  • 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);
    });