2

I'm having trouble figuring out how to create unique key names for my online election app. Here is a typical set of relevant entities:

  • 1 Election
  • 5 Candidates
  • 100,000 Voters
  • up to 100,000 Votes

I need a unique key name for each Voter entity for the reasons expressed here. An email address would be unique for a voter in a particular election but not necessarily across multiple elections (since the same voter could be part of multiple elections).

If the Election was the parent entity of the voter entities, then using the voter email in combination with the election parent would be sufficient for a unique key. Since I need to use transactions in updating Voter entities (to make sure that a voter cannot vote more than once) I think I don't want to use an entity group because only one Voter could be updated at a time according to the docs.

Another solution could be to create a unique key for each voter of the form:

E<election ID>_<voter email>

e.g.,

E1001_john@example.com

Is that a reasonable way to create a unique key name for each Voter entity?

Community
  • 1
  • 1
new name
  • 15,861
  • 19
  • 68
  • 114
  • 1
    Per your previous question, you already have a unique uuid field for each voter. Why not use that as the key name? – Nick Johnson Jul 18 '11 at 01:40
  • @Nick, I create the UUID's using the Python uuid package, any my understanding is that they are not guaranteed to be unique (though will be with very high probability). I perform checks to make sure they are unique within an election but not between other elections. I think I would still need to prepend an election ID (or something similar) to ensure uniqueness. – new name Jul 18 '11 at 02:12
  • @Nick, I just did some more reading on UUIDs and it seems the probability of collision is so low that I shouldn't worry about it. – new name Jul 18 '11 at 02:26
  • You second option looks fine (=uniq) – Igor Artamonov Jul 18 '11 at 12:42

2 Answers2

3

Double-check your assumptions. As it is, there is a situation that could hamper the ID's uniqueness:

What if two voters use the same e-mail address? I know that my parents only have a single one between them. As it is, your ID would clash.

There are bunches of way to minimize collisions. If worst comes to worst, you could hash some of the voter's information. Shoot, you could even hash the exact time at which you're creating the ID.

(Hm... I guess I kind of like hashing things...)

Simplest solution? Do what people in databases do for IDs: a number. Every time you add a new voter, increment the number. The first voter you add will have an ID of 1, the second will have an id of 2, and so on. If you need to, you can convert the id to a string.

But, don't assume that data is unique. Two people can have the same name, or street address, or e-mail address, or telephone number, etc.

Hope that gives you some ideas!

Daniel Ralston
  • 2,230
  • 21
  • 15
  • For other reasons, my app requires each voter to have a unique email address. But I like the idea to use 1 to N instead of voter emails. – new name Jul 18 '11 at 01:18
0

That should be unique. I would probably use a GUID myself, and tie it back to a data repository with a unique constraint on the voting session and voter, but that's just a personal preference.

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254