Short snippet to restore product variation assignments (references) in Drupal 8 Commerce after an SQL error has occured on product save, which left product variations orphaned.
Fix all product variation relationships having no parent product and set the correct product ID (here "10"):
UPDATE `commerce_product_variation_field_data` SET product_id=10 WHERE product_id IS NULL;
Build back-references:
SET @rn=0;
INSERT INTO commerce_product__variations SELECT type, '0' as deleted, product_id as entity_id, '10' as revision_id, langcode, @rn:=@rn+1 AS delta, variation_id FROM `commerce_product_variation_field_data` WHERE product_id=10 ORDER BY `variation_id` ASC;