How to include filter in relation belong-to, ManyToOne or one to one
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
Use scope to define filters for relations:
find({
"include":[
{
"relation":"insured",
"scope": {
"where":{
"insurance_name": "Mickel"
}
}
}
]
});
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?
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 can you write down your solution here, if you can?
Steps to make work search using a related object with annotation @HasMany @BelongTo @OneToOne using MYSQL
- 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;`
- After been applied the migration you can perform searching with where
{
"include":[
{
"relation":"insured",
"scope":{
"where":{
"insured_city":"Carora"
}
}
}
]
}
Closing as solved based on https://github.com/strongloop/loopback-next/issues/6237#issuecomment-687671581.