Does RethinkDb support AUTO_INCREMENT on a integer column similar to AUTO_INCREMENT in Mysql. https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
4 Answers
No, RethinkDB doesn't support it. The reason is because of its distribution. It's hard to have an auto increment number on such that environment because you have to check on multiple machines for next incremental value.
Now, let's think what problem auto increment solve? On MySQL, We want to use it for primary key so it needs to be unique. That's all about it. Auto increment doesn't give you anything else.
In RethinkDB, UUID guarantees the uniqueness, especially in the case of primary key.
Auto Increment is also predictable, probably it isn't cause any harmful but ideally, it gives people a sense of what's next value to attack. For example, take a poor design application, where we can visit some URL like /this/is/a/sensitive/part/123, someone can hit /this/is/a/sensitive/part/124. Of course, this is the fault of application for not having a solid authentication system. However, UUID may help reduce this a bit because UUID isn't predictable.

- 2,304
- 2
- 14
- 9
-
62 downsides of UUID: 1) You still need to check for collision. 2) The generated UUID itself is long and thus not suitable for use in sharable urls. – Dave May 17 '16 at 20:59
-
It's also not necessarily a security flaw to have an auto-incrementing integer column. There are plenty of use cases where one might want it to provide an absolute ordering over all records. A UUID can still be used as the publicly accessible ID of the record. – Endophage Oct 26 '16 at 23:52
-
One way to get autoincrement seems to be, to get a count of all users/entities and then just add 1. You'd have to increment values yourself, but this should be trivial.. – Qasim May 06 '17 at 06:57
-
Auto Increment can be set for non-primary key fields for a reason; when you need things to be in sequential order. – evilReiko Jun 08 '17 at 09:45
we archive like below example
```
r.db('autoInc').table('testauto')
.insert(r.do(function() {
return {
autoVal:r.branch(
r.db('FlowzEngine').table('task_worker').isEmpty().not()
.and(r.db('FlowzEngine').
table('task_worker').hasFields('autoVal')
.isEmpty().not()),
r.db('FlowzEngine').table('task_worker')
.hasFields('autoVal').max('autoVal')
.do(function(doc){
return doc('autoVal').add(1)
}),
1)}
}))
```

- 313
- 1
- 9
you can achieve this using a counter table like below:
r.table('counters')
.get(id)
.update({
count: r.row('count')
.default(0)
.add(1)
}).run(conn, callback);

- 1,778
- 16
- 21
An example based on Chirag's answer, for php, if using Daniel Mewes php-rql..
Chirag's answer for some reason is showing using separate tables, this isn't required, not sure if it's an oversight in the example or not..
$result = \r\table('user')
->insert(array(
'username' => $username, /* other data for example */
'password' => $password,
'userId' =>
\r\rDo(null, function($arg) {
return \r\branch(
\r\table('user') /* if */
->isEmpty()->not()
->rAnd(
\r\table('user')
->hasFields('userId')
->isEmpty()->not()
),
\r\table('user') /* then */
->hasFields('userId')
->max('userId')
->rDo(function($arg) {
return $arg('userId')->add(1);
}),
1); /* else */
})
))
->run($db);
I thought I would add it as it took me more than a few minutes to get this working on PHP, syntactical differences can be annoying.

- 66
- 5