I have a class
Now, lets say I add three competitor products to a product x using x.addToCompetitorProducts, It will insert three records into the product_competitor_products table.
Now, I do x.competitorProducts.clear() and then re-add the same three competitor products to x using x.addToCompetitorProducts
I dont know why, but it creates duplicate records of same competitor products for product x in the table. After calling save, there will be six records in the table, instead of just three. The three records will be duplicate.
Please note - It will work in tests and can not be reproduced in tests, That said
If you do following in tests, it will pass without any issues, It will show you just three competitor products if you debug the running application and inspect product.competitorProducts, but if you check in database, there will be six records and not three.
following test will pass, but if you run app in dev env, and do the same, there will be six records in db.
Note - It works if you add different competitor records rather then the same after doing clear(), and it will create just three records in database, (removing old three records and adding three new)
Please look at these threads