loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

How to include filter in relation belong-to, ManyToOne or one to one

Open georve opened this issue 6 years ago • 6 comments

i have an entity which has a relationship with another.. example

@model({settings: {strict: true}}) export class Quotation extends Entity {

@belongsTo(() => Agent) agentId: number;

@belongsTo(() => Insured) insuredId: number; }

i'm trying to filter insured using relationshiop but it always bring to me all the data

find( { "include":[ { "relation":"insured", "where":{ "insurance_name":'Mickel' } } ] })

there is a way to filter the object related to include relationship

georve avatar Sep 01 '20 04:09 georve

Use scope to define filters for relations:

find({
  "include":[
    {
      "relation":"insured",
      "scope": {
        "where":{
          "insurance_name": "Mickel"
        }
      }
    }
  ]
});

achrinza avatar Sep 01 '20 06:09 achrinza

thank you for your response, I try but I'm getting all record.

{ "include":[ { "relation":"insured", "scope":{ "where":{ "insured_city":"Carora" }
} } ] }

I'm working with Mysql connector, Is there an issue with this connector? or maybe a fix is needed?

georve avatar Sep 01 '20 14:09 georve

finally, i solve the issue making the include of foreign-keys into the loopback model. I saw in the source code that the class use the foreign keys and it help me to solve the filter

georve avatar Sep 05 '20 22:09 georve

@georve can you write down your solution here, if you can?

puranjayjain-mtxb2b avatar Sep 08 '20 08:09 puranjayjain-mtxb2b

Steps to make work search using a related object with annotation @HasMany @BelongTo @OneToOne using MYSQL

  1. Make the relationship using foreign keys with the object-related `@model({settings: { strict: true, foreignKeys:{ fk_policy_enterpriseId:{ name:'fk_policy_enterpriseId', entity:'Enterprise', entityKey:'id', foreignKey:'enterpriseId', onDelete: 'cascade' }, fk_policy_agentId:{ name:'fk_policy_agentId', entity:'Agent', entityKey:'id', foreignKey:'agentId', onDelete: 'cascade' }, fk_policy_agencyId:{ name:'fk_policy_agencyId', entity:'Agency', entityKey:'agency_code', foreignKey:'agencyId', onDelete: 'cascade' }, fk_policy_productId:{ name:'fk_policy_productId', entity:'Products', entityKey:'id', foreignKey:'productsId', onDelete: 'cascade' }, fk_policy_insuredId:{ name:'fk_policy_insuredId', entity:'Insured', entityKey:'id', foreignKey:'insuredId', onDelete: 'cascade' }, }, }}) export class Policy extends Entity { @property({ type: 'string', id: true, generated: false, required: true, }) policy_number: string;

@property({ type: 'number', }) insuredId?: number;

@property({ type: 'string', }) agencyId?: string;

@property({ type: 'number', }) agentId?: number;

@property({ type: 'number', }) enterpriseId?: number;`

  1. After been applied the migration you can perform searching with where

{ "include":[ { "relation":"insured", "scope":{ "where":{ "insured_city":"Carora" } } } ] }

georve avatar Sep 11 '20 14:09 georve

Closing as solved based on https://github.com/strongloop/loopback-next/issues/6237#issuecomment-687671581.

achrinza avatar Sep 15 '20 10:09 achrinza