recursive many to many relationship loopback - loopbackjs

I was trying to make hasMany relation of model with the same model itself like my model was ticket and in that relations defined are parentTickets and childTickets which are array of tickets and i made a mapping table 'ticketRelation' which is mapping table for the has many relationship.My models are following-
ticket model-
"relations":{
"parentTickets":{
"type":"hasMany",
"model":"ticket",
"foreignKey":"childId",
"through":"ticketRelation"
},
"childTickets":{
"type":"hasMany",
"model":"ticket",
"foreignKey":"parentId",
"through":"ticketRelation"
}
}
ticketRelation-
"relations":{
"pticket": {
"type": "belongsTo",
"model": "ticket",
"foreignKey": "parentId"
},
"ticket": {
"type": "belongsTo",
"model": "ticket",
"foreignKey": "childId"
}
}
My sample data is-
ticket id =1 has child tickets with id =2,3
so when i try to find parentTickets in ticket model by the following URL
http//localhost:3000/api/tickets?filter[include]=childTickets
it give me correct result ie ticket-id =1,childTickets=2,3
but whenever i try to find parentTickets for ticket by the following URL, it is not giving me correct result
http//localhost:3000/api/tickets?filter[include]=parentTickets
The data retrieved is-
ticket-id=1, parentTicket -1
so the problem i noticed is might be that loopback is expecting the relation name to be same as that of model name which we are specifying in the relation in the mapping table( ticketRelation) to retrieve the data.

Related

Django differ JSONField values between lists and objects

I am using django 3.2 with Postgres as DB.
I have a model with JSONField:
class MyModel(models.Model):
data = models.JSONField(default=dict, blank=True)
In database there are a lot of records in this table and some data have JSON values as object and others as lists:
{
"0:00:00 A": "text",
"0:01:00 B": "text",
"0:02:00 C": "text",
}
[
{"time": "0:00:00", "type": "A", "description": "text"},
{"time": "0:01:00", "type": "B", "description": "text"},
{"time": "0:02:00", "type": "C", "description": "text"},
]
I need to filter all records which has JSON values as objects.
What I tried is to use has_key with time frame "0:00:00" :
result = MyModel.objects.filter(data__has_key="0:00:00 A")
But I really cant use it because I am not sure what the key with time frame look like completely.
Any ideas how to filter JSONField values by object struct?

searchContacts with phone number query is broken

Using the [searchContacts API method] (https://developers.google.com/people/api/rest/v1/people/searchContacts) used to support searching by telephone number - indeed this is called out in the documentation:
The query matches on a contact's names, nickNames, emailAddresses, phoneNumbers, and organizations fields that are from the CONTACT source.
It no longer returns results when using a phone number as the query. Is this deliberate, or a bug?
As per google people api search by phonenumbers I have tried a query of "canonical format without plus". I have also tried "canonical format with plus" and "exact number as stored".
Name query still works
https://people.googleapis.com/v1/people:searchContacts?readMask=names%2cphoneNumbers&query=Go Ogle&pageSize=30
returns
{
"results": [
{
"person": {
"resourceName": "people/c832768086350305259",
"etag": "%EgcBAgsuNz0/GgECIgwxZGVYd20reHpEUT0=",
"names": [
{
"metadata": {
"primary": true,
"source": {
"type": "CONTACT",
"id": "b8e96298f3117eb"
}
},
"displayName": "Go Ogle",
"familyName": "Ogle",
"givenName": "Go",
"displayNameLastFirst": "Ogle, Go",
"unstructuredName": "Go Ogle"
}
],
"phoneNumbers": [
{
"metadata": {
"primary": true,
"source": {
"type": "CONTACT",
"id": "b8e96298f3117eb"
}
},
"value": "020 7031 3000",
"canonicalForm": "+442070313000"
}
]
}
}
]
}
Phone number query fails
https://people.googleapis.com/v1/people:searchContacts?readMask=names%2cphoneNumbers&query=442070313000&pageSize=30
returns
{}
The query function does indeed seem to be broken at the moment. My tests gave the same results and the question you linked shows that it clearly worked in the past.
I found a bug report on Google's issue tracker. A Googler already replied to it saying that they were able to reproduce it and filed an internal report. It's a matter of time until they fix it so you may want to keep track of that thread or post on it yourself to apply some pressure.
The bug didn't went away although they say it was closed and verified
In order to get the same functionality I had to be creative, the documentation says:
The query matches on a contact's names, nickNames, emailAddresses,
phoneNumbers, and organizations fields that are from the CONTACT
source.
The names, emailAddresses, phoneNumbers and organizations are important fields where you don't want to have garbage, but on my case at least the nickNames had no usage, so I simply add the phone number as nick name and the search works like charm.
Keep in mind that if you have many previous contacts you will have to write a script that will copy their phone number to the one of the nicknames fields.
Enjoy :)

Strongloop - hasOne relation

I am having some trouble with setting up a hasOne relation, which probably comes from me understanding the relation wrongly?
I have 2 Models, one user model, and one location model. What I want to add now is a relation between user and location, meaning a user has a current location. But if I set up a hasOne relation on the user model with the location, I end up with a userId property in the location. But this is completely wrong, since several users can have the same current location, so the user model should store the location id, not the location the user id. So how can I achieve what I want, so that I can afterwards query the user and include the current location?
I can of course add a property to the user and store the id of the location there, but then I can't as easily include the location in a user request. So I would prefer using relations to achieve this.
Your problem is a bit unclear, given the comments of Brian's post, but if you absolutely need Users to share a given set of locations then you would be better off using User belongsTo location.
This will create a locationId field in User, and you will be able to GET api\Users\{userId}\location
Eventually, you can set a location hasMany User to retrieve from a given location all the users in there
The relation name is referring to the relational meaning of "has one," which means that for each user, there exists one (and only one) entry in the location table. Each user "has one" entry in the location table, and if your data needs to show that two users have the same location, it just means the location table would store identical location data with different userIds. This is still perfectly fine for relational mapping, and allows you to do User.location calls.
What you are looking for is slightly different, which would be "Location hasMany Users," because you will be sharing location entries with multiple users. Read this as "for each location entry, many users could share it." You'll have to query a bit differently and use the include: ['location'] filter when you want to return the User with location data included (otherwise you'll only get the locationId value).
Relation builder:
$ slc loopback:relation
? Select the model to create the relationship from: Location
? Relation type: has many
? Choose a model to create a relationship with: user
? Enter the property name for the relation: users
? Optionally enter a custom foreign key:
? Require a through model? No
location.json:
{
"name": "Location",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"lat": {
"type": "number"
},
"long": {
"type": "number"
}
},
"validations": [],
"relations": {
"users": {
"type": "hasMany",
"model": "user",
"foreignKey": "locationId"
}
},
"acls": [],
"methods": {}
}

Why does FQL return different user IDs to Graph API

I want to see which users can see a particular album. I use the following query to get my albums and their privacy settings:
SELECT id,allow,deny,description,friends,value FROM privacy WHERE id IN
(SELECT object_id FROM album WHERE owner=me())
and it returns something like this for each album:
{
"id": 212102865605678,
"allow": "211824028961234,212367312342178",
"deny": null,
"description": "John Doe, Work People",
"friends": "SOME_FRIENDS",
"value": "CUSTOM"
}
The second ID in the allow column is right. That's the ID for the 'Work People' friendlist.
But the other ID isn't really the ID of 'John Doe'.
If I use the FB.api from javascript using 'me/friends' I get this:
{
"name": "John Doe",
"id": "100005318169867"
}
Same name, but different ID. I'm quite certain that they're the same user (I don't have many friends). Is this the difference between some internal database ID and external ID?
Any help would be greatly appreciated!

Django - serialize to JSON, how to serialize a FK User object

I'm on a page which sends a request to server side for a list of comments of current topic.
like this
localhost:8000/getComments/567 , while 567 is the topic id.
then, in my django view, like this:
def getProjectComments(request, pId):
format = 'json'
mimetype = 'application/javascript' #'application/xml' for 'xml' format
comments = PrjComment.objects.filter(prj=pId)
data = serializers.serialize(format, comments)
return HttpResponse(data,mimetype)
Now , the question is ,
when I try to use jQuery.parseJSON(data) at the browser side.
[
{
"pk": 10,
"model": "app.prjcomment",
"fields":
{
"status": 1,
"time_Commented": "2011-12-11 17:23:56",
"prj": 1,
"content": "my comment 1",
"user": 25
}
},
{
"pk": 9,
"model": "app.prjcomment",
"fields": {
"status": 1,
"time_Commented": "2011-12-11 17:23:51",
"prj": 1,
"content": "comment \u4e00\u4e2a",
"user": 33
}
} ..
I need to use some detail information of user object. (user is a Foreign Key in model PrjComment)
such as user.first_name to display on the comment list.
but here it is only an id for user.("user": 33)
How can I do that? Anyone who can kind help?
Thank you very much
the User is the Django auth_user.
The easy solution would be to specify the values you need:
comments = PrjComment.objects.filter(prj=pId) \
.values('status','time_commented','prj','content','user__id',
'user__username','user__first_name')
Update:
To access your userprofile information use the table name as defined in your AUTH_PROFILE_MODULE setting. In my case the table is called userprofile, and I'd access the data like this:
values('user__userprofile__moreinfo')
where moreinfo is the field you're interested in