Cheok Yan Cheng
Cheok Yan Cheng

Reputation: 42710

Way to ensure unique constraint of a field

I have the following model class.

class Human(db.Model):
    email = db.StringProperty(required=True)
    date = db.DateTimeProperty(auto_now=True)
    checksum = db.IntegerProperty(required=True)
    version = db.IntegerProperty(required=True)
    content = blobstore.BlobReferenceProperty(required=True)

Currently, to ensure the uniqueness of email in database level, (Ensure there are no duplicated email in entire database) I am using the following method.

    h = human.Human(key_name='[email protected]', email='[email protected]', checksum=456, version=1281, content=blob_key)

I am not sure this is a good way to do so? Or, is there any other better way?

Upvotes: 0

Views: 100

Answers (1)

Wooble
Wooble

Reputation: 89897

This is really the only way to do it.

The email property is probably redundant in this case, since you're already storing the data in the key name.

The only other option is to give all of the Human entities the same parent, thus putting them together in an entity group, which will allow you to do updates within a transaction to check for an existing entity with the same email. However, this would prevent you from making more than about 1 change to all of your Human entities (and any of their children) per second, which doesn't sound bad for a small low-traffic site but will kill scalability.

Upvotes: 3

Related Questions