Bulk insert records with foreign key

Hi,

I'm trying to insert records in 2 tables

  • 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 :
Capture d’écran 2024-05-28 à 18.20.18

I have tried additionalScope :
Capture d’écran 2024-05-28 à 18.19.33

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
  • or with passing a correct additionalScope

Thanks in advance

Hi @MFP - similar question and resolution here:

Take a look and let us know if you need any further help!

Hi Jg,

Thanks for looking, but the question is 'similar', but not same.
In this post, the question is 1 parent 1 child, so it's fine.

My problem is to address several children records, which make things quite different ...

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:

insertListeEcritures.trigger(
  {additionalScope: { currentLignesList.value.map(obj => ({
                        ...obj,
                        ecritureId: ecriture[0].id
                      }))
  }
});

OR (if you create the variable in your script)

insertListeEcritures.trigger(
  {additionalScope: { childrenInsertArray } }
);

Insert code might not be 100% depending on how this are setup in your App, but the "code to play around with" should help you get there.

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) ...

Are you trying to update the currentLignesList variable? If so, you could use:

currentLignesList.setValue(
  currentLignesList.value.map((obj) => ({
    ...obj,
    ecriture: ecriture[0].id,
  }))
);