I have a pretty simple hasMany relationship where a "product" hasMany "packages". I'm running a script to try and fill in the data, which works fine on the first run. However on the second run, it starts inputing duplicates, which I though was not possible for a hasMany
package = Package.findBySourceId(packageId) ?: new Package(name:packageName, price:packagePrice, sourceId:packageId).save(flush:true)
product = Product.findBySourceId(productId)
product.addToPackages(package)
product.save(flush:true)
When I put on sql logging on, I can see that sometimes the select that should run on the addToPackages call is not run. It just does the select to find the product and then a straight insert into the join table.
I don't want to add the exact queries because of work but basically its like
Select -> for the initial package which it finds
Select -> for product which it finds by the product id
Insert -> insert into the join table without even a select to check if an entry exists
However sometimes instead of that insert it will run a select and find the match and the process will start over. No idea on the logic going on behind grails here. Keep in mind, I have a script running that's pulling the data with thousands of line, is this a session issue somehow?