How to preserve the external ids of product templates and variants when migrating data?
Old database
product .attribute and product.attribute.value
Export the old attributes and their values. Suppose we have 2 attributes, Size and Color and 2 values for each, namely S and L and red and blue.
Size | S |
L | |
Color | red |
blue |
product.product
Export variant data with attribute values and the external ids of both the variant as well as the corresponding product template. Suppose we have 2 types of t-shirts in our database with each 4 variants, S,blue, S,red, L,blue and L,red.
product_templ_id/id |
id | product_template_attribute_value_ids |
---|---|---|
__export__.product_template_1 | __export__.product_product_11 | S,blue |
__export__.product_template_1 | __export__.product_product_12 | S,red |
__export__.product_template_1 | __export__.product_product_13 | L,blue |
__export__.product_template_1 | __export__.product_product_14 | L,red |
__export__.product_template_2 | __export__.product_product_21 | S,blue |
__export__.product_template_2 | __export__.product_product_22 | S,red |
__export__.product_template_2 | __export__.product_product_23 | L,blue |
__export__.product_template_2 | __export__.product_product_24 | L,red |
New database
product.attribute
Set the default value for the Variants Creation Mode (create_variant) to Dynamically (dynamic) or see to it that this value is set too during the import. This way no variants will be created automatically when importing the product templates. We want to import the variants ourselves and link them to the right product template.
product.attribute.value
Just import the old values into the new database.
product.template
Import the 2 product templates first. In our example this looks like ...
id | name | attribute_line_ids/attribute_id | attribute_line_ids/value_ids |
---|---|---|---|
__export__.product_template_1 | T-shirt 1 | Size | S,L |
Color | red, blue | ||
__export__.product_template_2 | T-shirt 2 | Size | S,L |
Color | red, blue |
product.template.attribute.value
This will create 8 records in the product.template.attribute.value table. (There is no default menu to access this model. Just add it with Studio.)
Record ID | product_templ_id/external_id |
value |
---|---|---|
1 | __export__.product_template_1 | S |
2 | __export__.product_template_1 | L |
3 | __export__.product_template_1 | red |
4 | __export__.product_template_1 | blue |
5 | __export__.product_template_2 | S |
6 | __export__.product_template_2 | L |
7 | __export__.product_template_2 | red |
8 | __export__.product_template_2 | blue |
We would need to reference these records when importing our variants. Best is to create external id's for these records ourselves. External id's don't exist until the external id's for records are exported. So export the records without external id's. If you accidentally did, no worries, you can delete them anytime. (If you delete the ir.model.data model be careful to keep Odoo defaults.)
ir.model.data
Make a combination with the number of the product template and the value. These values are known in your original export from the old database and so the external identifiers can be constructed there to import the variants themselves eventually.
Record ID | External Identifier | Model |
Module |
---|---|---|---|
1 | __export__.product_template_attribute_value_1_S | product.template.attribute.value | __export__ |
2 | __export__.product_template_attribute_value_1_L | product.template.attribute.value | __export__ |
3 | __export__.product_template_attribute_value_1_red | product.template.attribute.value | __export__ |
4 | __export__.product_template_attribute_value_1_blue | product.template.attribute.value | __export__ |
5 | __export__.product_template_attribute_value_2_S | product.template.attribute.value | __export__ |
6 | __export__.product_template_attribute_value_2_L | product.template.attribute.value | __export__ |
7 | __export__.product_template_attribute_value_2_red | product.template.attribute.value | __export__ |
8 | __export__.product_template_attribute_value_2_blue | product.template.attribute.value | __export__ |
Note that existing records can't be updated so if you work in batches, you would need to keep track of non-existing external ids. It's a lot easier to delete them all first and import the expanded list again.
product.product
Don't just use S,blue as value for the product_template_attribute_value_ids. S,blue is not unique. For template 1 it will be __export__.product_template_attribute_value_1_S,__export__.product_template_attribute_value_1_blue and for template 2 it will be __export__.product_template_attribute_value_2_S,__export__.product_template_attribute_value_2_blue.
product_templ_id/id |
id | product_template_attribute_value_ids | |
---|---|---|---|
__export__.product_template_1 | __export__.product_product_11 | S,blue | __export__.product_template_attribute_value_ 1_S,__export__.product_template_attribute_value_ 1_blue |
__export__.product_template_1 | __export__.product_product_12 | S,red | __export__.product_template_attribute_value_ 1_S,__export__.product_template_attribute_value_ 1_red |
__export__.product_template_1 | __export__.product_product_13 | L,blue | __export__.product_template_attribute_value_ 1_L,__export__.product_template_attribute_value_ 1_blue |
__export__.product_template_1 | __export__.product_product_14 | L,red | __export__.product_template_attribute_value_ 1_L,__export__.product_template_attribute_value_ 1_red |
__export__.product_template_2 | __export__.product_product_21 | S,blue | __export__.product_template_attribute_value_ 2_S,__export__.product_template_attribute_value_ 2_blue |
__export__.product_template_2 | __export__.product_product_22 | S,red | __export__.product_template_attribute_value_ 2_S,__export__.product_template_attribute_value_ 2_red |
__export__.product_template_2 | __export__.product_product_23 | L,blue | __export__.product_template_attribute_value_ 2_L,__export__.product_template_attribute_value_ 2_blue |
__export__.product_template_2 | __export__.product_product_24 | L,red | __export__.product_template_attribute_value_ 2_L,__export__.product_template_attribute_value_ 2_red |