i have a Contrato model that has many Item model and in my Item model it has a Produto model.
So, basic, my Item belongs to one Produto and my Item belongs to one Contrato, but a Contrato can have a lot of Items. The problem is that i want to get all the Contratos with the Items and on each Item the Produto.
After reading the documentation, it seens the case:
/customers?filter[include][reviews]=author
But the result is that the Contratos has the items but each item only has the id of the Produto and not the Produto object itself.. What i am doing wrong?
Here is the response that i am receiving:
[
{
"id": 1,
"codigoTemplate": null,
"status": "aguardando",
"prazoFinalizacao": "2017-11-17T02:00:00.000Z",
"dataInicio": "2017-10-01T23:47:10.000Z",
"dataFinalizacao": null,
"emailComissao": "email#hotmail.com",
"dono": null,
"usuario": 2,
"itens": [
{
"id": 1,
"quantidade": 5,
"contrato": 1,
"produto": 1
},
{
"id": 2,
"quantidade": 3,
"contrato": 1,
"produto": 2
}
]
}
]
My Contrato realtions:
"relations": {
"formandos": {
"type": "hasMany",
"model": "formando",
"foreignKey": "contrato"
},
"itens": {
"type": "hasMany",
"model": "item",
"foreignKey": "contrato"
},
"usuario": {
"type": "belongsTo",
"model": "usuario",
"foreignKey": "usuario"
}
}
My Item relations:
"relations": {
"itemFormando": {
"type": "hasOne",
"model": "itemFormando",
"foreignKey": "item"
},
"produto":{
"type": "belongsTo",
"model": "produto",
"foreignKey": "produto"
}
}
And my Produto relations:
"relations": {
"item": {
"type": "hasOne",
"model": "item",
"foreignKey": "produto"
}
}
sorry cannot put a comment,
As in your Contrato Model hasMany Itens and in your Item model you didn't mention that your item belongsTo Contrato which is fine,
So try to remove the relation in produto and keep the relation in Item model because one relation is sufficient.
After that call it in include with the scope :
in your find function :
include: {
relation: 'produto',
scope: {
fields: ['Fields that you want to call'],
},
},
Hope it helps
Related
I am using Loopback 3 and i have the following three models:
UserFile.json
{
"name": "UserFile",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"id": {
"type": "number",
"id": true,
"generated": true
},
"name": {
"type": "string",
"required": true
},
"size": {
"type": "number",
"default": 0
},
"uploadedAt": {
"type": "date",
"default": "$now"
}
},
"relations": {
"hasFile": {
"type": "hasMany",
"model": "UploadedFile",
"foreignKey": "fileId"
}
}
}
UploadedFile.json
{
"name": "UploadedFile",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"hashId": {
"type": "string",
"required": true
},
......
},
"relations": {
"file": {
"type": "belongsTo",
"model": "UserFile",
"foreignKey": "fileId"
}
}
}
PartitionedFile.json
{
"name": "PartitionedFile",
"base": "UploadedFile",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"partSize": {
"type": "number",
"default": 0
}
},
"relations": {
"file": {
"type": "belongsTo",
"model": "UserFile",
"foreignKey": "fileId"
}
}
}
The idea is that the UserFile has either one UploadedFile or (two or more) many PartitionedFile.
I want to be able to get from the UserFile, from the same relation the file, whether it is an UploadedFile or a PartitionedFile. Is it possible to achieve this just from the models' definitions or it can only be done with a remote method?
A relation from a model to another is bound to only one model; so you cannot have one relation called "file" to point to different models. In your example, it's bound to UploadedFile. In order for your model UserFile to have relations with both UploadedFile and PartitionedFile, you'll need two different relations.
For your UserFile to have either one UploadedFile or many PartitionedFile, it needs to have them both within its relations:
{
"name": "UserFile",
...
"relations": {
"uploadedFiles": {
"type": "hasOne", // Zero on one uploaded file
"model": "UploadedFile",
"foreignKey": "uploadedFileId"
},
"partitionedFiles": {
"type": "hasMany", // Zero or many partitioned files
"model": "PartitionedFile"
}
}
}
Then, to retrieve files from both UploadedFiles and PartitionedFiles through UserFile, you don't need a remote, but to include the models within your request.
For example, in ReactJS, it would look like this:
const response = await Axios.get(`/api/UserFile`, {
params: {
filter: {
include: [
{ relation: 'uploadedFiles' },
{ relation: 'partitionedFiles' },
]
}
}
}
Another solution would be to specify another model File that contains all these relations (UploadedFile, PartitionedFile, etc), so that the model UserFile can refer to the table File as one unique relation to retrieve the files...
I'm looking into Loopback and stuck in finding a solution when two models (let's say Customer/Orders) have a one-to-many relationship but the primary key (id) of the parent isn't a foreign key in child table, but rather some non-key field.
For example, consider following two tables
Customer[ id(pk), name, social_security_number]
Order [ id(pk), social_security_number(fk)]
I'm not able to create the relationship based on the above scenario. Loopback isn't returning orders when I query against a particular customer. Here is my actual code snippet (partial). I'm using default id field as Primary Key
{
"name": "Customer",
"properties": {
"name": {
"type": "string",
"required": true
},
"social_security_number": {
"type": "number",
"required": true
}
},
"relations": {
"orders": {
"type": "hasMany",
"model": "Order",
"foreignKey": "social_security_number"
}
}
}
{
"name": "Order",
"base": "PersistedModel",
"properties": {
"social_security_number": {
"type": "number",
"required": true
}
},
"relations": {
"customer": {
"type": "belongsTo",
"model": "Customer",
"foreignKey": "social_security_number"
}
}
}
How can I make this work?
I have a Car model with the following relation:
...
"relations": {
"belongsToUser": {
"type": "belongsTo",
"model": "MyUser",
"foreignKey": ""
},
"shop": {
"type": "belongsTo",
"model": "Shop",
"foreignKey": ""
}
}
Now, if I try this request:
http://localhost:5000/api/cars?filter={"include": ["belongsToUser"]}
It doesn't include the user related to the cars.
If I try this request:
`http://localhost:5000/api/cars?filter={"include": ["shop"]}`
It does return the shop correctly. What's wrong with the user inclusion?
I have an Order model which hasMany OrderItem models. But once a client wants to create an Order, it has to create an Order object first then for each product he added to his basket, he needs to create responding OrderItems separately. As you may notice it causes many reduntant requests. May be I can make a custom method for OrderItems which consumes a product list. But i was wondering if there is a built in mechanism for this like createMany since it is a very useful operation.
ORDER MODEL
{
"name": "Order",
"plural": "Orders",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"customerId": {
"type": "number",
"required": true
},
"branchId": {
"type": "number",
"required": true
}
},
"validations": [],
"relations": {
"orderItems": {
"type": "hasMany",
"model": "OrderItem",
"foreignKey": "orderId"
}
},
"acls": [],
"methods": []
}
ORDERITEM MODEL
{
"name": "OrderItem",
"plural": "OrderItems",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"UnitPrice": {
"type": "number"
},
"productId": {
"type": "number",
"required": true
},
"purchaseOrderId": {
"type": "number",
"required": true
},
"quantity": {
"type": "number"
}
},
"validations": [],
"relations": {
"product": {
"type": "belongsTo",
"model": "Product",
"foreignKey": "productId"
},
"purchaseOrder": {
"type": "belongsTo",
"model": "PurchaseOrder",
"foreignKey": ""
}
},
"acls": [],
"methods": []
}
Loopback "create" method accepts also an array of objects (see PersistedModel.create docs) so you should try creating one "create" call and send an array of OrderItems.
I have two Entities: Post and Tag, with a ManyToMany relation called 'tagged'
What I need to do, in order to parse json returned on particular client, is output fields by adding a field 'postID' (the pk field of current Post) to Tag related to that post.
So, my output now is:
{
"post": {
"name": "Dummy name",
"pk": 1,
"tags": [
{
"id": 2,
"name": "Patio"
},
{
"id": 3,
"name": "Roof"
}
],
"ref": "709230056"
}
},
but it should be:
{
"post": {
"name": "Dummy name",
"pk": 1,
"tags": [
{
"id": 2,
"name": "Patio",
"postID": 1,
},
{
"id": 3,
"name": "Roof"
"postID": 1,
}
],
"ref": "709230056"
}
},
I've alrady tried to play with fields of my Handler but with no success :(
How to do that ?
Thanks