Snippet: Drupal 8 Commerce: Product variation assign in DB

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;

Comments

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.

More information about formatting options