-
Hello everybody, I'm experiencing a strange behavior with the many to many relationship in my application.
Obviously the outsourcing partner and the billing group have a many to many relationship. I recieve data from an API that returns the outsourcing partners with all their billing groups, but only some og the relations are getting saved. My models are looking like this: class OutsourcingPartner extends Model {
static entity = 'outsourcingPartners';
@Uid()
declare id: string;
@Str('')
declare name: string;
@BelongsToMany(
() => BillingGroup,
() => OutsourcingPartnerBillingGroup,
'outsourcingPartner_id',
'billingGroup_id'
)
declare billingGroups: BillingGroup[];
}
class BillingGroup extends Model {
static entity = 'billingGroups';
@Uid()
declare id: string;
@Str('')
declare name: string;
}
class OutsourcingPartnerBillingGroup extends Model {
static entity = 'outsourcingPartnerBillingGroups';
static primaryKey = ['outsourcingPartner_id', 'billingGroup_id'];
@Uid()
declare outsourcingPartner_id: string;
@Uid()
declare billingGroup_id: string;
} The data that I recieve from my API and pass to the [
{
"id":"36a11c85380b4afb994710ea63f99964",
"name":"Luke Skywalker",
"billingGroups":[
{
"id":"c8d8db2b1f4f4a069fa640c9c8cbe489",
"name":"The Jedis",
"__typename":"BillingGroup"
}
],
"__typename":"OutsourcingPartner"
},
{
"id":"658b19b76a0f494590c21a7c27173d8e",
"name":"Darth Vader",
"billingGroups":[
{
"id":"95a87597ef124bbf9907f8f6074f8e94",
"name":"Dark Forces",
"__typename":"BillingGroup"
}
],
"__typename":"OutsourcingPartner"
},
{
"id":"fabaecbb6ac045bb9b582244b889d309",
"name":"Yoda",
"billingGroups":[
{
"id":"4dedf55e5a8a4ce5affb1bc65b65de09",
"name":"Jedi Council",
"__typename":"BillingGroup"
},
{
"id":"c8d8db2b1f4f4a069fa640c9c8cbe489",
"name":"The Jedis",
"__typename":"BillingGroup"
}
],
"__typename":"OutsourcingPartner"
},
{
"id":"0284241ba3ed487eb8d12c0e86161fc0",
"name":"Clone Trooper 0815",
"billingGroups":[
{
"id":"95a87597ef124bbf9907f8f6074f8e94",
"name":"Dark Forces",
"__typename":"BillingGroup"
}
],
"__typename":"OutsourcingPartner"
}
] The result from the [
{
"id": "36a11c85380b4afb994710ea63f99964",
"name": "Luke Skywalker",
"billingGroups": [
{
"id": "c8d8db2b1f4f4a069fa640c9c8cbe489",
"name": "The Jedis",
"pivot_outsourcingPartnerBillingGroups": null
}
]
},
{
"id": "658b19b76a0f494590c21a7c27173d8e",
"name": "Darth Vader",
"billingGroups": [
{
"id": "95a87597ef124bbf9907f8f6074f8e94",
"name": "Dark Forces",
"pivot_outsourcingPartnerBillingGroups": null
}
]
},
{
"id": "fabaecbb6ac045bb9b582244b889d309",
"name": "Yoda",
"billingGroups": [
{
"id": "4dedf55e5a8a4ce5affb1bc65b65de09",
"name": "Jedi Council",
"pivot_outsourcingPartnerBillingGroups": {
"outsourcingPartner_id": "fabaecbb6ac045bb9b582244b889d309",
"billingGroup_id": "4dedf55e5a8a4ce5affb1bc65b65de09"
}
},
{
"id": "c8d8db2b1f4f4a069fa640c9c8cbe489",
"name": "The Jedis",
"pivot_outsourcingPartnerBillingGroups": null
}
]
},
{
"id": "0284241ba3ed487eb8d12c0e86161fc0",
"name": "Clone Trooper 0815",
"billingGroups": [
{
"id": "95a87597ef124bbf9907f8f6074f8e94",
"name": "Dark Forces",
"pivot_outsourcingPartnerBillingGroups": null
}
]
}
] And the state store is looking like this after the call to the {
"outsourcingPartners": {
"data": {
"36a11c85380b4afb994710ea63f99964": {
"id": "36a11c85380b4afb994710ea63f99964",
"name": "Luke Skywalker"
},
"658b19b76a0f494590c21a7c27173d8e": {
"id": "658b19b76a0f494590c21a7c27173d8e",
"name": "Darth Vader"
},
"fabaecbb6ac045bb9b582244b889d309": {
"id": "fabaecbb6ac045bb9b582244b889d309",
"name": "Yoda"
},
"0284241ba3ed487eb8d12c0e86161fc0": {
"id": "0284241ba3ed487eb8d12c0e86161fc0",
"name": "Clone Trooper 0815"
}
}
},
"outsourcingPartnerBillingGroups": {
"data": {
"[\"36a11c85380b4afb994710ea63f99964\",\"c8d8db2b1f4f4a069fa640c9c8cbe489\"]": {
"outsourcingPartner_id": "36a11c85380b4afb994710ea63f99964",
"billingGroup_id": "c8d8db2b1f4f4a069fa640c9c8cbe489"
},
"[\"658b19b76a0f494590c21a7c27173d8e\",\"95a87597ef124bbf9907f8f6074f8e94\"]": {
"outsourcingPartner_id": "658b19b76a0f494590c21a7c27173d8e",
"billingGroup_id": "95a87597ef124bbf9907f8f6074f8e94"
},
"[\"fabaecbb6ac045bb9b582244b889d309\",\"4dedf55e5a8a4ce5affb1bc65b65de09\"]": {
"outsourcingPartner_id": "fabaecbb6ac045bb9b582244b889d309",
"billingGroup_id": "4dedf55e5a8a4ce5affb1bc65b65de09"
}
}
},
"billingGroups": {
"data": {
"c8d8db2b1f4f4a069fa640c9c8cbe489": {
"id": "c8d8db2b1f4f4a069fa640c9c8cbe489",
"name": "The Jedis"
},
"95a87597ef124bbf9907f8f6074f8e94": {
"id": "95a87597ef124bbf9907f8f6074f8e94",
"name": "Dark Forces"
},
"4dedf55e5a8a4ce5affb1bc65b65de09": {
"id": "4dedf55e5a8a4ce5affb1bc65b65de09",
"name": "Jedi Council"
}
}
}
} So as you can see, only three relations are getting stored, but I have five in total. And also if I query the model from the repo, I only recieve three relations. I'm pretty sure there is some kind of misconfiguration from my side, but I don't know where. Any help is really appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 22 replies
-
Hey @Marvin-S , i tried your code and got the right result: {
outsourcingPartnerBillingGroups: {
data: {
'["36a11c85380b4afb994710ea63f99964","c8d8db2b1f4f4a069fa640c9c8cbe489"]': [Object],
'["658b19b76a0f494590c21a7c27173d8e","95a87597ef124bbf9907f8f6074f8e94"]': [Object],
'["fabaecbb6ac045bb9b582244b889d309","4dedf55e5a8a4ce5affb1bc65b65de09"]': [Object],
'["fabaecbb6ac045bb9b582244b889d309","c8d8db2b1f4f4a069fa640c9c8cbe489"]': [Object],
'["0284241ba3ed487eb8d12c0e86161fc0","95a87597ef124bbf9907f8f6074f8e94"]': [Object]
}
},
billingGroups: {
data: {
c8d8db2b1f4f4a069fa640c9c8cbe489: [Object],
'95a87597ef124bbf9907f8f6074f8e94': [Object],
'4dedf55e5a8a4ce5affb1bc65b65de09': [Object]
}
},
outsourcingPartners: {
data: {
'36a11c85380b4afb994710ea63f99964': [Object],
'658b19b76a0f494590c21a7c27173d8e': [Object],
fabaecbb6ac045bb9b582244b889d309: [Object],
'0284241ba3ed487eb8d12c0e86161fc0': [Object]
}
}
} If you can give me a working reproduction then i can help you. 🙏 Right now it seems some important information is missing. |
Beta Was this translation helpful? Give feedback.
Hey @Marvin-S ,
i tried your code and got the right result: