0

I have event table in Corvid Database, it has Event_Id column for every new event which is not asked from the user in the form.

I used the following code to get the number of rows and generate id from that which works good now:

    let count = 0;
  $w("#collection").onReady( () => {
     count = $w("#collection").getTotalCount(); // 23
       count++; 
  } );

$w('#btnSub').onClick( ()=>{
const newRequest = {
    eventid:('event_'+count),
    title: $w('#title').value
}
wixData.insert('event_instance', newRequest);

but this can lead to duplication of event id as I delete one of the row from collection

Can you please find a solution for this?

Thanks

Stein Åsmul
  • 39,960
  • 25
  • 91
  • 164
Engr Umair
  • 130
  • 10

2 Answers2

0

You need to use dropdowns in #myElementID

This similar to this in SQL.

if not exists (select * from Delegates d where d.FromYr = @FromYr and d.MemNo = @MemNo) INSERT INTO Delegates ([MemNo],[FromYr],[ToYr]) values(@MemNo, @FromYr,@ToYr)

Else refer this

champion-runner
  • 1,489
  • 1
  • 13
  • 26
0

Make sense get count before each insert

$w('#collection').onReady( () => {
  $w('#btnSub').onClick( () => {
    const count = $w('#collection').getTotalCount() + 1; // here

    wixData.insert('event_instance', {
      eventid: ('event_' + count),
      title: $w('#title').value
    });
  });
});
  • if I add 3 items, so it will give a count of 3 and if I delete the second element, then it will give me a count of 2 and my two events will be event 3 and event 3 – Engr Umair Jan 24 '20 at 08:48