Hello
Feature(s) impacted
collection.addField()
Observed behavior
collection.addField('npPlan', {
columnType: 'String',
dependencies: ['np_contract:fk_plan:title'],
getValues: (records, context) =>
records.map(r => {
if (r.np_contract && r.np_contract.fk_plan) {
return r.nv_contract.fk_plan.title;
}
}),
});
With the above code I’m trying to add a field, using data from a relationship that is itself in another relationship that is linked to my beneficiaries table.
However, my table np_contract is null, which causes the following error
Expected behavior
If the np_contract is null then leave a null and don’t continue looking for fk_plan
Context
Project name: Nostrum Care v3
Team name: Op
Environment name: Local
Agent (forest package) name & version:
“@forestadmin /agent”: “^1.0.0”,
“@forestadmin /datasource-sequelize”: “^1.5.21”,
“@forestadmin /datasource-sql”: “^1.0.0”,
“dotenv”: “^16.0.1”,
“pg”: “^8.8.0”,
“sequelize”: “^6.33.0”,
“stripe”: “^14.17.0”
Database type: Postgresql
Thanks in advance for your feedback!
okay thanks for the information
Hello @jacques_liao ,
Sadly as @Enki pointed out, this is currently not supported, I’ll investigate if it is a bug a never meant to be supported.
In the meantime I can suggest another workaround: you can manually complete the import of the field
collection
.addField('npPlan', {
columnType: 'String',
dependencies: ['contractId'],
getValues: async (records, context) => {
if (records.length === 0) return [];
const contractIds = records
.map(record => record.contractId)
.filter(contractId => Boolean(contractId));
if (contractIds.length === 0) return [];
const planFromContractId = await context.dataSource
.getCollection('fk_plan')
.nativeDriver.rawQuery(
`
SELECT c.id, c.title FROM
"contracts" c
INNER JOIN "fk_plans" p ON c."planId" = p.id
WHERE c.id IN(:contractIds)
`,
{
contractIds,
},
);
return records.map(({ contractId }) => {
if (!contractId) return '';
const plan = planFromContractId.find(c => c.id === contractId);
return `${plan.title}`;
});
},
})
The given example may require some changes but the idea is here.
I’ll check it out. Thanks @dogan.ay
And is it the same for relationships?
For example, below I’m creating a relationship between my users table and np_subscription .
collection.addOneToOneRelation('np_subscription', 'NP_subscriptions', {
originKey: 'fk_beneficiaryId',
originKeyTarget: 'id',
});
when I check on forest it displays the relationships correctly, but when I click on a particular line I get an error. However, I haven’t added any fields to my users table.
I don’t have any specific log related to this error apart from this log
warning: [404] GET /forest/users/ffe6cbe6-2555-4cbf-a7e6-8f1be12eb4db - 107ms
That should not be the case for relationships.
If the relationship is displayed properly when viewing data from the list, I would suspect something else. From what you shared, I do not see any error in the way you defined the one to one relationship.
Do you see anything in your browser logs or network tab when selecting the user ?
Hello @jacques_liao ,
You may have a look at the import field functionality . It’s a boiler plate that allows you to add a field from a belongsTo/hasOne relationship.
Something like:
collection
.importField('contractPlanTitle', { path: 'np_contract:plan:title', readonly: true });
Please note that the agent support Autocompletion & Typings that could be handy to use the right path.
Regards,
Morgan
yes I have an error 500 for the get information and the error in the screen below
And in the network I have the same error 500
Hello @morganperre ,
for collection.importField() if, for example, np_contract is null, will it return an error or will it leave the field I imported null ?
I’m sorry to have this kind of question, but at the moment I can’t really test it on my own, as I’m on my own.
Hello @jacques_liao ,
jacques_liao:
for collection.importField() if, for example, np_contract is null, will it return an error or will it leave the field I imported null ?
It should handle the case correctly and just let null
in this case. You can easily test locally this with a little setup.
Regards,
Morgan
yes, it’s true! I’ll test it later.
Hello, I was able to try
collection
.importField('npPlan', { path: 'np_contract:fk_plan:title', readonly: true });
But I get the following error saying that it can’t find fk_plan , and currently in the np_contract table I have no data. And because np_contract is null , collection.importField() can’t pass that I think
Hello, do you any news ? is it better to do it via a debug point @morganperre ?
Hello @jacques_liao ,
I juste try and I have achieved a 2 level import (through 2 relationships).
collection.importField('emailThroughPostAndUser', {
path: 'post:user:email',
readonly: true,
})
So in your case, you can try with np_contract:plan:title
and not fk_plan
.
collection
.importField('contractPlanTitle', { path: 'np_contract:plan:title', readonly: true });
Let me know. I will open an issue for the typing issue.
Kind regards,
Morgan
jacques_liao:
I have the same errors
This is normal, you don’t have plan field. I don’t know your fields / relation name. You can have a look into your typing file maybe it will help you.
But for sure it works. (In my case I also had some null
relation and everything works perfectly)
Regards,
I have the correct fk_plan field in the nv_contract table, but it doesn’t seem to be detected.
Hey @jacques_liao ,
You seem in troubles here, could you please book a call with me?
https://meetings-eu1.hubspot.com/lionel-bouzonville/cx-session
Before reaching this call, we will appreciate if you can follow this guide to update your system and ease the process. Thanks!
2 Likes
Hello
Problem solved thanks to you , the problem came from my side, I’ll be more careful next time !
2 Likes