Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 309863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:46:51+00:00 2026-05-12T07:46:51+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T07:46:51+00:00Added an answer on May 12, 2026 at 7:46 am

    There’s no built-in constraint for making sure a value is unique. You can do this however:

    query = MyModel.all(keys_only=True).filter('unique_property', value_to_be_used)
    entity = query.get()
    if entity:
        raise Exception('unique_property must have a unique value!')
    

    I use keys_only=True because it’ll improve the performance slightly by not fetching the data for the entity.

    A more efficient method would be to use a separate model with no fields whose key name is made up of property name + value. Then you could use get_by_key_name to fetch one or more of these composite key names and if you get one or more not-None values, you know there are duplicate values (and checking which values were not None, you’ll know which ones were not unique.)


    As onebyone mentioned in the comments, these approaches – by their get first, put later nature – run the risk concurrency issues. Theoretically, an entity could be created just after the check for an existing value, and then the code after the check will still execute, leading to duplicate values. To prevent this, you will have to use transactions: Transactions – Google App Engine


    If you’re looking to check for uniqueness across all entities with transactions, you’d have to put all of them in the same group using the first method, which would be very inefficient. For transactions, use the second method like this:

    class UniqueConstraint(db.Model):
        @classmethod
        def check(cls, model, **values):
            # Create a pseudo-key for use as an entity group.
            parent = db.Key.from_path(model.kind(), 'unique-values')
    
            # Build a list of key names to test.
            key_names = []
            for key in values:
                key_names.append('%s:%s' % (key, values[key]))
    
            def txn():
                result = cls.get_by_key_name(key_names, parent)
                for test in result:
                    if test: return False
                for key_name in key_names:
                    uc = cls(key_name=key_name, parent=parent)
                    uc.put()
                return True
    
            return db.run_in_transaction(txn)
    

    UniqueConstraint.check(...) will assume that every single key/value pair must be unique to return success. The transaction will use a single entity group for every model kind. This way, the transaction is reliable for several different fields at once (for only one field, this would be much simpler.) Also, even if you’ve got fields with the same name in one or more models, they will not conflict with each other.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am using .net win forms i need to set some common properties globally
I'd like to subclass UIButton to add some properties that i need (not methods...
I was using an mxml class but since i need to pass some properties
I need to change some custom properties values in many files. Here is an
I need to expose some input fields based on what properties I find for
In some scenarios I need a wide version of an entity with many properties
Need some help to solve this. I have a gridview and inside the gridview
Need some guidance figuring out what went wrong. I've been using mysql, phpmyadmin for
Need some help with this problem in implementing with XSLT, I had already implemented
Need some help gathering thoughts on this issue. Our team is moving ahead with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.