views:

1098

answers:

1

I need some properties to be unique. How can I achieve this?

Is there something like unique=True?

I'm using Google App Engine for Python.

+5  A: 
Blixt
Don't these approaches have problems with concurrency? You could do the check, but then a concurrent request could create a duplicate before you get around to persisting your entity.
Steve Jessop
It's possible to put these in transactions, but I'll leave that to the OP.
Blixt
You can't put a query in a transaction unless it has an ancestor filter, and can only operate on entities in the same entity group. So both approaches require everything to be in the same group, which is a lot, and will make Google (more precisely, datastore's distribution of your entities across nodes) sad. The best I can think of is get_or_insert into a table of values, together with a large random value so you can figure out whether yours was the one inserted. If so, then the name is "yours".
Steve Jessop
@onebyone: I updated my post with code to show what I meant with the second method. It does use very broad entity groups, but I would hope he's not doing more than five inserting `put`s per second per model.
Blixt