1

I'm studing DynamoDB but I don't understand how can I create a unique user's id foreach user. In SQL is simple, there is an int (AI) field. It'll be a int value because each user's profile url is like (very common form):

my-application-url/users/[:id_users]
ex. my-application-url/users/1/

My old save_users function is:

public function save_users($users){ 
 if($users->id == 0){
    $this->tableGateway()->save($users);
 }else{
    $this->tableGateway()->update($users, 'id_users' => (int) $users->id);
 }
}

I use zend framework 2.

I could get the last registered user's id and increment it, but if I have 2 or more simultaneus registrations, I could get 2 or more identical user's id.

the users dynamodb table it also simple:

{
  "id_users" Number: 1,
  "firstname" String: "Alessandro",
  "lastname" String: "Corradini",
  "email" String: "my-email",
  "password" String: "my-encrypted-password"
}

POSSIBLE SOLUTION

I need a UUID field into users table. The table 'll be like:

{
  "UUID" String : <UUID-placeholder>,
  "id_users" Number: 1,
  "firstname" String: "Alessandro",
  "lastname" String: "Corradini",
  "email" String: "my-email",
  "password" String: "my-encrypted-password"
}

For id_users identifier, also unique, I'll generate a fixed lenght id_users with this function below. Before saving user into register.php, I'll check if is present into table (and email also!!), if is not, save user, else generate another unique id.

 function randomId($length){
    $result = mt_rand(1, 9);

    for($i = 0; $i < ($length - 1); $i++) {
        $result .= mt_rand(0, 9);
    }

    return $result;
 }

For 16 character length, I have 10^16 combinations. I think that is enaugh.

Community
  • 1
  • 1

1 Answers1

1

the approach in dynamodb is to create an UUID in your client side, for example by timestamp (instead of auto-increment feature)

you can also use an external service that provide you the id (by using locks on global verb)

you can get more information here:

Are autoincremented primary keys okay https://forums.aws.amazon.com/thread.jspa?messageID=561190

How to make a UUID in DynamoDB?

https://www.quora.com/How-to-auto-increment-in-DynamoDB

Community
  • 1
  • 1
Eyal Ch
  • 9,552
  • 5
  • 44
  • 54
  • ok, I'll ad a UUID item. For "id_users" field I create a function than return a long integer for a fixed lenght (ex. 16). `function randomId($length){ $result = mt_rand(1, 9); for($i = 0; $i < ($length - 1); $i++) { $result .= mt_rand(0, 9); } return $result; }` – Alessandro Corradini Mar 10 '16 at 16:15
  • I just add the solution in the first post because I can't format the comment :( . It's a correct solution? – Alessandro Corradini Mar 10 '16 at 16:34
  • Why not creating user id by timestamp? You can keep like this your user id incremented , so the higher user id is the latest created. You can also use composite (random +timestamp) – Eyal Ch Mar 10 '16 at 17:52