How can I create two unique, requested fields for the GAE data warehouse data model?

First, a little tweak. Last week, I had a problem implementing a specific methodology that I created that would allow me to manage two unique fields associated with a single db.Model object. Since this is not possible, I created a parent entity class and a class of child entities, each of which has a key name that is assigned one of the unique values. You can find my previous question located here , which includes my sample code and a general explanation of my insert process.

In my initial question, someone commented that my solution would not solve my problem of having to use two unique fields associated with a single db.Model object.

My implementation tried to solve this problem by implementing a static method that creates ParentEntity, and its key_name property is assigned to one of my unique values. In the second step of my process, I create a child and assign the parent to the parent parameter. Both of these steps are performed in the db transaction, so I assumed that this would make the uniqueness problem work, as both of my values ​​were stored in two separate key_name fields on two separate models.

The comment noted that this solution would not work because when you set the parent to the child, the key name is no longer unique to the entire model, but instead unique to the records of the parent and child. Bummer ...

I believe that I could solve this new problem by changing how these two models relate to each other.

, . , key_name. , . , . "--", , , .

- . GAE Datastore db , . , .

. ? , , . , . ? , , .

, . .

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()
0
2

, . , - , , .

, , . , Model EGEnforcer ( Entity Group Enforcer.)

. , , , .

EGEnforcer, . , , EGEnforcer , . , EGEnforcer, . ! .

* key_name * parent-key_name, , FirstEntity ( ParentEntity) . , SecondEntity ( ChildEntity) , _, .

, . - , .

#My new class containing unique entries for each pair of models associated within one another.
class EGEnforcer(db.Model): 
KEY_NAME_EXAMPLE = 'arbitrary unique value'

    @staticmethod
    setup():
        ''' This only needs to be called once for the lifetime of the application. setup() inserts a record into EGEnforcer that will be used as a parent for FirstEntity and SecondEntity entries.  '''
        ege = EGEnforcer.get_or_insert(EGEnforcer.KEY_NAME_EXAMPLE)
    return ege

class FirstEntity(db.Model):
    str1_key =  db.StringProperty()
    str2 =      db.StringProperty()

    @staticmethod
    def InsertData(string1, string2, string3):
        try:
            def txn():
                ege = EGEnforcer.get_by_key_name(EGEnforcer.KEY_NAME_EXAMPLE)
                prt = FirstEntity(
                    key_name=string1, 
                    parent=ege) #Our EGEnforcer record.
                prt.put()

                child = SecondEntity(
                    key_name=string2, 
                    parent=ege, #Our EGEnforcer record.
                    parentEnt=prt,
                    str1=string1, 
                    str2_key=string2,
                    str3=string3)
                child.put()
                return child
        #This works because our entities are now part of the same entity group
            db.run_in_transaction(txn)
        except Exception, e:
            raise e

class SecondEntity(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)

#Other data...
    str3 =      db.StringProperty()

- :

- , , , , " ", , , .

, , , , . . , , Google. , , Google, SecondEntity ( UserAccount .) , .

, , , - . -, , . , .

, , , , . , .

0

, , . , - ReferenceProperty.

- , , , , " ", , , , . , , , . (, 30 , ).

, , - , . , .

+1

All Articles