I have two entities: Cars (ownerId->String) and Users (userId->String), where ownerId is the foreign key representing userId.
I am trying to execute this Query, but I get "GQL syntax" at Datastore Viewer. How can I do this SUBSELECT?
SELECT * FROM Cars WHERE ownerId IN (SELECT userId FROM Users);
You can't. GQL is not SQL.
You will need to to do one select, then iterate and fetch/filter or do two selects and merge in code.
Alternately add a flag in the Car object that tells you that the Owner is a User, then you don't need to
If you are not trying to write code, but just explore then the remote_api shell is a place to try these things. Though this is much easier in python ;-)
Related
I can select all apex triggers by using
select id, name from ApexTrigger
and classes by using
select id, name from ApexClasses
but is there a way to select all objects ?
I have tried
select id, name from Objects but doesnt seem to work ?
SOQL queries run on exactly one object.
If you need to perform a full-text search across multiple objects, what you need is SOSL, the Salesforce Object Search Language. Note however that SOSL is quite different from SOQL. See the linked developer guide for more information.
I am trying to fetch some entities with distinct values on a property. Say, I have an entity called messages. And it has some properties say personId, typeId, convId, createdTime, etc. I want fetch messages of personId p1 with distinct convIds. How can I do that.
I already referred to
Executing DISTINCT query with objectify for app engine
and some others. And tried something like this.
ofy().load().type(messages.class).limit( 10 ).filter("personId ==", "p1").order("-createdTime").project("convId").distinct(true).list();
I am sure that there are some entities with this combination. But it is fetching no entities.
Please help me with this.
Unfortunately, your query isn't valid. For projection queries you need distinct to be on the values that you are sorting by (i.e. -createdTime). To use distinct you'll need to order by convId.
https://cloud.google.com/datastore/docs/concepts/queries#restrictions_on_queries has more details.
I am unable to get the user information from LoginHistory object.
I tried using this
SELECT User.FirstName FROM LoginHistory
but not it says
INVALID_FIELD:
SELECT User.FirstName FROM LoginHistory
^
ERROR at Row:1:Column:8
Didn't understand relationship 'User' in field path. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names.
System.debug(JSON.serializePretty(LoginHistory.UserId.getDescribe())); shows "null" as the "relationshipName". Not all relations allow going "up" this way, you can check my answer https://salesforce.stackexchange.com/a/23507/799 for some examples.
You'll need to make-do with two separate queries. There's equally no way to reverse it because the related list doesn't have name either. This won't work:
SELECT FirstName,
(SELECT Id FROM LoginHistories LIMIT 10)
FROM User
LIMIT 10
Maybe there's an idea you can upvote? Maybe you'll have more luck with Event Monitoring. Haven't used it personally but my understanding is it can track login, logout, exporting a report... Might be easier to query.
I'm trying to write a SOQL query that pulls back User information along with the DefaultDivision's name. When executing the following:
Select u.Email, u.FirstName, u.LastName, u.DefaultDivision from User u
the value for 'DefaultDivision' is an Id (like 02d3498202020222) instead of the friendly name (like North America). I know that the DefaultDivision maps to the Division object's Id, but how do I get the name?
I have tried things like:
select u.Email, (select Name from Division where Id = u.DefaultDivision) from User u
but that doesn't work because there is no direct relationship (and yet, there is...!).
Unlike SQL, SOQL does not support subqueries of the type you have given there.
In most cases with SOQL where there is an explicit relationship defined, you can query data from that explicit relationship directly, like
select u.Email, u.DefaultDivision.Name from User u where Id=blahblahblah
However there are some cases where such a relationship is not explicitly defined, and I believe DefaultDivision is one of those (so in fact my query above probably will not work). When this is the case, there's no single-query way to get the information you want -- you'll have to first query on divisions and their IDs, then query users with their DefaultDivision, and then resolve the division names from the IDs using the results of the first query.
Fortunately this type of situation doesn't happen very often anymore, but in using Divisions you're plumbing the depths of Salesforce.com a bit so you may find some unusual stuff there.
Given the following Many to Many Relationship designed in Google App Engine Datastore:
User
PK: UserID
Name
Company
PK: CompanyID
Name
CompanyReview
CK CompanyID
CK UserID
ReviewContent
For optimization query, what's the best way to query this relationship tables for showing the selected company's review by users.
Currently, I'm doing the following:
results = CompanyReview.all().filter('owned_by = ', company).filter('written_by = ', user).fetch(10)
where I'm able to retrieve the data of CompanyReview table. However, in this case, I would need to check against the UserID from this CompanyReview table against the User table in order to obtain the name of the users who have commented for the selected company.
Is there a better solution to grab the user name as well, all in one statement in this case or at least better optimized solution? Performance is emphasized.
It dependes on which side of the relationship will have more values. As described is this article of Google App Engine docs, you can model many-to-many relationships by using a list of keys in one side of the relationship. "This means you should place the list on side of the relationship which you expect to have fewer values".
If both sides of the relationship will have many values, then you will really need the CompanyReview model. But pay attention to what the article says:
However, you need to be very careful because traversing the
connections of a collection will require more calls to the datastore.
Use this kind of many-to-many relationship only when you really need
to, and do so with care to the performance of your application.
This is because it uses RefereceProperty in the relationship model:
class ContactCompany(db.Model):
contact = db.ReferenceProperty(Contact,
required=True,
collection_name='companies')
company = db.ReferenceProperty(Company,
required=True,
collection_name='contacts')
title = db.StringProperty()
So if in Contact entities we try to access the companies, it will make a new query. And if in ContactCompany entities we try to get attributes of contact as in contact_company.contact.name, a query for that single contact will be made also. Read the ReferencyProperty docs for more info.
Extra:
Since you are performance-savvy, I recommend using a decorator for memcaching function returns and using this excellent layered storage library for Google App Engine.