I use go with app engine and datastore. I have Restaurants and Owners entities. Owner is parent to Restaurant. I show list of restaurants on main web page. When visitor clicks on a restaurant, the profile page(www.../restaurants/:id) will open. I don't have ancestor key at that point. How should I retrieve the restaurant? I could use /restaurants/:key but datastore.key is quite long string.
What is the natural way of doing this? should I use unique restaurant names and query/filter by that?
You can keep your current data model (restaurants child entities of owners). All you need is to add "Owner" variable to a restaurant.
When you retrieve restaurant entity, you can extract owner ID from its key - every key already includes a parent key, if any. In Java it looks like this:
restaurant.setOwner(entity.getParent().getId());
When you need to get/save the restaurant entity, you can create its full key using its own identifier and a parent key, created with owner ID.
Related
My question is suppose there is one parent user in the database and inside that user, there is a particular fieldName like fieldName: "mainstream" and I want to add objects of users inside him in an array that will have the unique name of their team as his fieldName and the user should be head of all the users inside him I am actually confused how can I build a schema for this requirement can any one help me, please?
In MySQL I used auto-increment to generate an id for every user. I would like to create a similar user table in Google Datastore where the id for a user will be unique. According to these docs:https://cloud.google.com/appengine/docs/java/datastore/entities
System-allocated ID values are guaranteed unique to the entity group.
But according to this post: Ever see duplicate IDs when using Google App Engine and ndb? the id's are not unique. I need this id to be unique. It is confusing because in the docs it says the id is unique, but from this post it says the id is not unique it is the key that is unique. My objective is for no two users to have the same id. How can I guarantee this? I would prefer for the database to take care of this form me opposed to me having to create large ids manually using things such as uuids.
As Igor correctly observed, IDs are always unique as long as the entity has no parent.
I can't think of any reason to make user entities children of some other entities, so you are safe.
Note that IDs will not be sequential, as it helps to spread the load equally across the entire dataset - it's a by-product of how the Datastore is designed.
I have 2 custom objects in Salesforce.com
One is PersonAccount and one is Accounts.
Within the default "Account" object I have a field called user_id
PersonAccount acts as a junction table to link "Account" to Accounts
PersonAccount does a lookup in Person for user_id Lookup(Account)
How can I build a query to check something in Account to find all the matching items in Accounts?
Currently, Salesforce only permits a single level of nested queries. It cane be done like the following:
[SELECT ID, Name, Field1 from Object__c WHERE Id IN ( SELECT Id FROM Object2__c WHERE Field2 = 'SomeValue')]
However, with the junction object you don't actually need to use a nested query.
Unfortunately, your description isn't clear enough to understand your specific object set-up, so I am going to make some assumptions.
You have three objects, Accounts__c (your custom Accounts Objct), PersonAccount__c (your junction object), and Account (the default Account objects).
The PersonAccount__c object contains two lookup fields (for a true Junction, they should be Master-Detail). The first is to Accounts__c (we will call that lup_cust_accounts__c). The second is to Account (we will call that lup_account__c). [As an aside it is a really bad idea to have an Accounts and Account object. It is going to screw you up because Salesforce will automatically pluralize words and then you will be confused as to which is which.]
Salesforce allows dot relationship lookups in SOQL queries. So if you want to get the ID and Name from custom Accounts Objects when the associated Account object's Name is like "Test", you could do the following:
[SELECT lup_cust_accounts__r.Id, lup_cust_accounts__r.Name FROM PersonAccount__c WHERE lup_account__r.Name LIKE 'Test%'];
Notice the double underscore r instead of double underscore c? That is how you indicate a reference (lookup) rather than the specific field value.
I am writing an application in Google App Engine with python and I want to sort users and user posts into groups. Users will be able to tag a post with a group ID and then that post will be displayed on the group page.
I would also like to relate the users to the groups so that only members of a group can tag a post with that group ID and so that I can display all the users of a group on the side. I am wondering if it would be more efficient to have a property on the user which will have all of the groups listed (I am thinking max 10 or so) or would it be better to have a property on the Group model which lists all of the users (possibly a few hundred).
Is there much of a difference here?
Your data model should derive from the most likely use cases. What are you going to retrieve?
A. Show a list of groups to a user.
B. Show a list of users in a group.
Solution:
If only A, store unindexed list of groups in a property of a user entity.
If both, same as above but indexed.
If only B, store unindexed list of users in a property of a group entity.
NB: If you make a property indexed, you cannot put hundreds of user ids in it - it will lead to an exploding index.
In addition to the key_name I generate, I also would like to have some other property, which will act as id (I don't want to show key_name to the user). Can it be id? Or, how to generate unique value instead of id?
What I will do - I will generate a url with usage of that id and parent key name. If user clicks on this link, I'll need to find this datastore entity and update it.
Here is the current code.
Creation of the record:
item = Items.get_by_key_name(key_names=user_id, parent=person)
if item is None:
item = Items(key_name='id'+user_id, parent=person)
Getting the record:
item = Items.get_by_key_name(key_names=user_id, parent=person)
user_id is what should be hidden.
I could be probably wrong because your requirements are not clear, but for me you should pass just the key to the view using:
item.key()
then you could pass back the key to the controller and easily retrieve a given entity with:
item = Items.get(key)
Entities have exactly one of a key name or ID - never both. You could create an entity with a single ReferenceProperty pointing to your target entity, and use its ID as an identifier, but there really should be no reason not to reveal a key name to a user - a well authored app should not rely on this value remaining secret.
Note that it's trivially easy to extract the key name (and the rest of the information about a key) from the string encoded key.