user462455
user462455

Reputation: 13588

Auto increment in Rethinkdb

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

Upvotes: 4

Views: 3319

Answers (4)

forbiddenera
forbiddenera

Reputation: 158

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.

Upvotes: 0

Suben Saha
Suben Saha

Reputation: 1838

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

Upvotes: 0

Chirag
Chirag

Reputation: 313

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

```

Upvotes: 1

kureikain
kureikain

Reputation: 2314

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.

Upvotes: 4

Related Questions