cancel
Showing results for 
Search instead for 
Did you mean: 

Using Commit while Creating Multiple Records in CAP

former_member643578
Participant
0 Kudos

Hello All,

I have 5 records for creation so I am sending from UI to CAP using Promise.all().

    @BeforeCreate()
    public async validateHeaderBeforeCreate(@Req() oRequest: any): Promise<void> {
        let oTransaction: any = cds.transaction(oRequest);
//Need to increment partner_counter field
        let oPartnerQuery = SELECT.from("namespace_contract")
            .columns('max(partner_counter) as partner_counter');
        let aContractPartners = await oTransaction
            .run(oPartnerQuery);
        oRequest.data.partner_counter = Number (aContractPartners[0]?.partner_counter ? aContractPartners[0]?.partner_counter : 0) + 1;
    }
    @AfterCreate()
    public async afterCreatePartner(@Req() oRequest: any): Promise<void> {
        let oTransaction: any = cds.transaction(oRequest);
        await oTransaction.commit();
    }

I am using PostgreSQL Database.

Here partner_counter is key field for me.

While I am inserting 5 records. commit() is not working. 2 records inserted properly when inserting 3 record I am getting following error. Please correct me if I am using commit() in wrong way.

commit-issue.png

gregorw
Active Contributor
0 Kudos

If this occurs with cds-pg I would suggest the following:

  • Create an incident in the cds-pg project
  • Fork the project
  • Create a test case using the cds-pg data model (try to insert multiple Beers)
  • Send a pull request for the test case
  • Maybe solve the issue yourself and send a pull request with the patch

Accepted Solutions (0)

Answers (0)