4

Let's assume we have two models: User and Token. A user may have only one Token, and a Token can belong to only one user.

Coming from relational databases, the following seems like the way to do it:

class User(db.Model):
    name = db.StringProperty()

class Token(db.Model):
    name = db.StringProperty()
    owner = db.ReferenceProperty(User)

This will of course create User.token_set as well, which is a db.Query. But I'd really like to access the token with just, say, User.token instead of User.token_set.get(), so how about..

class User(db.Model):
    name = db.StringProperty()
    token = db.ReferenceProperty(Token)

class Token(db.Model):
    name = db.StringProperty()
    owner = db.ReferenceProperty(User)

Now I can access them in a bidirectional fashion: User.token & User.owner. And just ignore the fact that I have those automatically-created .token_set and user_set.

Is there anything wrong with this? On the logic side, on the performance side?

Maybe I shouldn't even have two models. They both, in practice, contain four-five properties. Should they just be one? When's a one-to-one relationship supposed to be ditched in just merged into one model?

Thanks for any input at all!

2 Answers 2

4

this is the appengine data store. it's not sql. you should group together data as it is used by your system. presumably you have places where users and tokens are used together, and since there are no issues with many-to-one these can go in the same model instance. this will give you better performance (much simpler retrieval of user by token, for example) and easier handling of consistency (updating token and user will be within a single transaction).

i don't know if self-linking is acceptable here, so perhaps someone will edit this if not, but i wrote http://acooke.org/cute/LessonsLea2.html to try collect together my experience with the data store. you might find it useful.

[edit: one way to think about this: you're not modeling data. you're writing a system that is intended to scale. that - and not modeling data - is what drives everything. i am not saying this is a good idea, but it's what appengine does. if your main priority is modeling data - for example, if you want to have multiple applications using your data in different ways - then you shouldn't be using appengine.]

1

Adding Token reference to user class has pros and cons. As benefit you will have better performance in token retrieval. As cons you will need more transactions when inserting data.

user = User(name='john').put()
token = Token(name='somehash', user=user).put()
user.token = token
user.put()

You also have to deal with null references when deleting tokens.

Perhaps you can go with class decorators if syntax sugar is what you want:

class User(db.Model):
    name = db.StringProperty()

    @property
    def token(self):
        if not hasattr(self, '_token_cached'):
            setattr(self, '_token_cached', self.token_set.get())
       return self._token_cached

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.