Auto-generate, auto-increment primary key

This is a little hard to answer without knowing your exact situation. It depends on how you’re creating the new item in Student Additional Info.

If you’re using a form or any other means that uses a dataset you can use the dataset’s onBeforeSave() function to register an event handler that add the StudentID to the item before it is saved.

That would look something like this:

$w("#StuAddDataset").onBeforeSave( () => {
   let studentId = // get the studentID here
   $w("#StuAddDataset").setFieldValue('studentId', studentId); 
} );

This approach is used in the Reviews example. Check out the code in the Post a Review lightbox. (Note: I just checked and the page for this example is currently showing the wrong example. It should be fixed soon.)

If you’re using the Data API to add the new item to Student Additional Info, you can use the API to add the studentId to the item as well.

Let me know if this helps or if your situation is not one that I though of.

Also, you might want to think about adding a feature request (or two) regarding auto-generated primary keys and working with foreign keys.

2 Likes