I have a user database with the following properties for each user:
- user
- id
- name
- zip
- city
In a relational database I would model it in a table user
:
- user
- id
- name
- location_id
and have a second table called location
:
- location
- id
- zip
- city
and location_id
is a foreign key (reference) to an entry in the location
table.
The advantage is, if the zip code for a certain city changes I only have to change one entry.
Let's go to the non-relational database, say Google App Engine. Here I would model it like in the specifications. I have a kind user
:
class User(db.Model):
name = db.StringProperty()
zip = db.StringProperty()
city = db.StringProperty()
The advantage is that I don't need to join two tables, but the disadvantage is that if the zip code changes I have to run a script that goes through all user entries and updates the zip code, correct?
There is another option in Google App Engine, which is to use ReferenceProperties
. I could have two kinds, user
and location
:
class Location(db.Model):
zip = db.StringProperty()
city = db.StringProperty()
class User(db.Model):
name = db.StringProperty()
location = db.ReferenceProperty(Location)
I have exactly the same model as in the relational database.
Is what I just did wrong?
Does that destroy all the advantages of a non-relational database?
In order to get the value of zip and city I have to run I second query. But in the other case, to make a change in the zip code I have to run through all existing users.
What are the implications of these two modeling possibilities in a non-relational database like Google's datastore?
What are typical use cases for both of them--when should I use one and when the other?