AnsweredAssumed Answered

Record creation, only if it does not yet exist

Question asked by Henry_2 on May 15, 2016
Latest reply on Sep 3, 2016 by Henry_2



In a record creation script I'm unsure how to prevent looping through records it has already created (preventing that would save a lot of time). Hope someone is able to help out:


I have these five tables (altered a bit for clarity):

1) stores (eg 'english store' and 'dutch store')

2) products

3) images

4) product_image_join (key fields are: productId, imageId)

5) product_image_join_store_specific (key fields are: productId, imageId, storeId)


Now for each record that exists in table 4 I want to create records in table 5, for each store. For example because the records would hold info whether to show the image for the store or not.


What does succeed?

I can create a script looping all records of table 4, and then for each store creating records in table 5. I currently create these records using table relations (connected three global fields of a selector table with the three key fields of table 5), I believe the method I use is also known as magic key. That runs reliable.
Unfortunately it will loop all, also those already created, simply because I don't check at all which still need creation or not.


My challenge

How do I check which have already been created and only create the new ones?


I was thinking about doing a sql query that would check which combinations do not yet exist and then create only those, I have not yet figured out just yet which steps to take exactly to get this working.


Hope someone is willing to help me out,