I am trying to do something that I was expecting to be very simple but is not working. Here’s the scenario: I have two collections, one (the “child”) has a reference to the other (the “parent”). The parent record may have none or one or more child records. (So this is a one-to-many relationship). At some point I need to delete all the related child records (if they exist) for a given parent record. In something outside of Wix this would be quite simple; you might use a SQL statement something like “DELETE ALL FROM [CHILDTABLE] WHERE PARENTID = [PARENTIDVALUE]” and all the related child records would be deleted, end of story. Maybe there’s something I’m not understanding but I would have expected something like "wixData.remove([COLLECTION], [ANYFIELD], [ANYVALUE]) so you could specify the foreign key field (the ParentID field) and specify the parent ID value. However, it looks like you can only specify the ID of the record you want to delete. So here’s my code (that’s not working):
//NOW USE THE ID OF THE PARENT RECORD JUST UPDATED
//AND DELETE ALL EXISTING CHILD RECORDS
wixData.query(“ServicesByLuthier”)
.eq(“fk_luthierBusinessService”, businessService._id)
.find()
.then((servicesByLuthier) => {
for (let i = 0; i < servicesByLuthier.items.length; i++) {
wixData.remove(“ServicesByLuthier”, servicesByLuthier.items[i]._id)
}
});
I am iterating through all the returned child records and trying to individually delete them, but it’s not working. I HAVE DETERMINED a) all existing child records for the parent ARE being returned b) the For Loop appears to be working as it should c) THE CORRECT PERMISSIONS are on the collection d) the correct ID is being passed to the remove function
Any ideas? Thank you in advance for any advice.