1 records into a parent table 'ecriture', say [ { id : xx , comment : "some comment"} ]
with the id being defined when record is inserted.
Several records into a children table 'lignes_ecritures', with foreign key to 'ecriture', say [ { id : aa , amount : 33 , ecriture : xx },
{ id : bb , amount 44 , ecriture : xx } ]
with the ecriture foreign key being the id of inserted 'ecriture' record
The records in the children table are stored in a variable 'currentLignesList'
like [ { amount : 33 , ecriture : null },
** { amount : 44 , ecriture : null } ]**
since ecriture FK is unknown before the 'ecriture' record is inserted
When I trigger the insert query, it works fine, but of course the FK stay null :
I have tried additionalScope :
But this does not work.
Any idea how I could bulk edit the FK either in :
the data array 'currentLignesList' once the parent 'ecriture' is created
Same concept; you need to map in the id to the children objects and pass the whole updated set as additional scope to your bulk insert.
//Code to play around with
// defining the parent key for code
let ecriture = [ { id : 01 , comment : "some comment"} ];
// for you, it will be generated by the insert
// fake child record set
let children = [ { id : 'aa' , amount : 33 },
{ id : 'bb' , amount : 44 } ]
let childrenInsertArray = children.map(obj => ({
...obj, // the child object
ecritureId: ecriture[0].id // the FK
}))
return childrenInsertArray //child record set with FK
To pass the updated array, make sure you have the additional scope variable setup in your insert, and then run:
I Assume if my children structure is { id : 'aa' , amount : 33, ecriture : ecriture[0].id }
the exact syntax would be :
as I understand this, I was wondering if there was a way of modifying directly the initial 'children' array with something like forEach.
(which I could not figure out, of course) ...