Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I create two unique, queriable fields for a GAE Datastore Data Model?
    primarykey
    data
    text
    <p>First a little setup. Last week I was having trouble implementing a specific methodology that I had constructed which would allow me to manage two unique fields associated with one db.Model object. Since this isn't possible, I created a parent entity class and a child entity class, each having the key_name assigned one of the unique values. You can find my previous question <a href="https://stackoverflow.com/questions/6550029/inserting-two-enties-in-a-transaction-getting-cannot-operate-on-different-enti">located here</a>, which includes my sample code and a general explaination of my insertion process. </p> <p>On my original question, someone commented that my solution would not solve my problem of needing two unique fields associated with one db.Model object.</p> <p>My implementation tried to solve this problem by implementing a static method that creates a ParentEntity and it's key_name property is assigned to one of my unique values. In step two of my process I create a child entity and assign the parent entity to the parent parameter. Both of these steps are executed within a db transaction so I assumed that this would force the uniqueness contraint to work since both of my values were stored within two, separate key_name fields across two separate models.</p> <p>The commenter pointed out that this solution would not work because when you set a parent to a child entity, the key_name is no longer unique across the entire model but, instead, is unique across the parent-child entries. Bummer...</p> <p>I believe that I could solve this new problem by changing how these two models are associated with one another.</p> <p>First, I create a parent object as mentioned above. Next, I create a child entity and assign my second, unique value to it's key_name. The difference is that the second entity has a reference property to the parent model. My first entity is assigned to the reference property but not to the parent parameter. This does not force a one-to-one reference but it does keep both of my values unique and I can manage the one-to-one nature of these objects so long as I can control the insertion process from within a transaction.</p> <p>This new solution is still problematic. According to the GAE Datastore documentation you can not execute multiple db updates in one transaction if the various entities within the update are not of the same entity group. Since I no longer make my first entity a parent of the second, they are no longer part of the same entity group and can not be inserted within the same transaction.</p> <p>I'm back to square one. What can I do to solve this problem? Specifically, what can I do to enforce two, unique values associated with one Model entity. As you can see, I am willing to get a bit creative. Can this be done? I know this will involve an out-of-the-box solution but there has to be a way.</p> <p>Below is my original code from my question I posted last week. I've added a few comments and code changes to implement my second attempt at solving this problem.</p> <pre><code>class ParentEntity(db.Model): str1_key = db.StringProperty() str2 = db.StringProperty() @staticmethod def InsertData(string1, string2, string3): try: def txn(): #create first entity prt = ParentEntity( key_name=string1, str1_key=string1, str2=string2) prt.put() #create User Account Entity child = ChildEntity( key_name=string2, #parent=prt, #My prt object was previously the parent of child parentEnt=prt, str1=string1, str2_key=string2, str3=string3,) child.put() return child #This should give me an error, b/c these two entities are no longer in the same entity group. :( db.run_in_transaction(txn) except Exception, e: raise e class ChildEntity(db.Model): #foreign and primary key values str1 = db.StringProperty() str2_key = db.StringProperty() #This is no longer a "parent" but a reference parentEnt = db.ReferenceProperty(reference_class=ParentEntity) #pertinent data below str3 = db.StringProperty() </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload