Unexpected 422 when trying to update product with new variants (metafields.key must be unique error)

Rares_Saftoiu
Shopify Partner
14 0 3

Hi there,

I am getting the following error when I am trying to update an item: {"errors":{"metafields.key":["must be unique within this namespace on this resource"]}}

I really don't understand why it's giving me this error. Here is the sequence of API calls (api version 2020-01)

1. First, I create a product by doing a POST to the /products.json endpoint:

 

{
    "title": "082520 - Test Product 1",
    "variants": [
        {
            "inventory_management": "shopify",
            "inventory_policy": "deny",
            "position": 1,
            "option1": "Variant 1",
            "price": "1.5",
            "requires_shipping": true,
            "taxable": true,
            "image_id": null,
            "metafields": [
                {
                    "value": "1231356115032215828",
                    "key": "variant_id",
                    "value_type": "string",
                    "namespace": "shopventory"
                }
            ]
        }
    ],
    "tags": [],
    "options": [
        {
            "name": "Variant",
            "values": [
                "Variant 1"
            ],
            "ordinal": 1
        }
    ]
}

 

2. I get back a successful response, this is the product/variant in Shopify (w/ metafields retrieved):

 

{
    "product": {
        "updated_at": "2020-08-25T15:45:34-07:00",
        "options": [
            {
                "values": [
                    "Variant 1"
                ],
                "id": 5817859768418,
                "product_id": 4488606810210,
                "name": "Variant",
                "position": 1
            }
        ],
        "title": "082520 - Test Product 1",
        "image": null,
        "body_html": null,
        "handle": "082520-test-product-1",
        "vendor": "rares-devel",
        "admin_graphql_api_id": "gid://shopify/Product/4488606810210",
        "images": [],
        "id": 4488606810210,
        "product_type": "",
        "variants": [
            {
                "option2": null,
                "sku": "",
                "price": "1.50",
                "title": "Variant 1",
                "requires_shipping": true,
                "compare_at_price": null,
                "option3": null,
                "id": 31900133064802,
                "taxable": true,
                "inventory_item_id": 33420786139234,
                "created_at": "2020-08-25T15:45:33-07:00",
                "product_id": 4488606810210,
                "weight": "0.0",
                "position": 1,
                "updated_at": "2020-08-25T15:45:33-07:00",
                "barcode": null,
                "inventory_policy": "deny",
                "weight_unit": "lb",
                "inventory_quantity": 0,
                "option1": "Variant 1",
                "admin_graphql_api_id": "gid://shopify/ProductVariant/31900133064802",
                "inventory_management": "shopify",
                "fulfillment_service": "manual",
                "image_id": null,
                "old_inventory_quantity": 0,
                "grams": 0,
                "metafields": [
                    {
                        "value": "1231356115032215828",
                        "key": "variant_id",
                        "id": 12368592863330,
                        "admin_graphql_api_id": "gid://shopify/Metafield/12368592863330",
                        "namespace": "shopventory",
                        "updated_at": "2020-08-25T15:45:33-07:00",
                        "owner_resource": "variant",
                        "created_at": "2020-08-25T15:45:33-07:00",
                        "description": null,
                        "value_type": "string",
                        "owner_id": 31900133064802
                    }
                ]
            }
        ],
        "template_suffix": null,
        "created_at": "2020-08-25T15:45:33-07:00",
        "tags": "",
        "published_scope": "global",
        "published_at": "2020-08-25T15:45:33-07:00"
    }
}

 

3. Then, I try to update the product with additional variants (a PUT to /products/4488606810210.json):

 

{
    "title": "082520 - Test Product 1",
    "image": null,
    "attributes": [],
    "handle": "082520-test-product-1",
    "vendor": "rares-devel",
    "images": [],
    "id": "4488606810210",
    "template_suffix": null,
    "created_at": "2020-08-25T15:45:33-07:00",
    "tags": [],
    "updated_at": "2020-08-25T15:45:40-07:00",
    "options": [
        {
            "name": "Variant",
            "values": [
                "Variant 3",
                "Variant 1",
                "Variant 2",
                "Variant 4"
            ],
            "ordinal": 1
        }
    ],
    "body_html": null,
    "product_type": "",
    "admin_graphql_api_id": "gid://shopify/Product/4488606810210",
    "published_at": "2020-08-25T15:45:33-07:00",
    "variants": [
        {
            "inventory_management": "shopify",
            "inventory_policy": "deny",
            "position": 1,
            "option1": "Variant 1",
            "price": "1",
            "requires_shipping": true,
            "taxable": true,
            "image_id": null,
            "metafields": [
                {
                    "value": "1231357730283852068",
                    "key": "variant_id",
                    "value_type": "string",
                    "namespace": "shopventory"
                }
            ]
        },
        {
            "inventory_management": "shopify",
            "inventory_policy": "deny",
            "position": 2,
            "option1": "Variant 2",
            "price": "2",
            "requires_shipping": true,
            "taxable": true,
            "image_id": null,
            "metafields": [
                {
                    "value": "1231357731147878696",
                    "key": "variant_id",
                    "value_type": "string",
                    "namespace": "shopventory"
                }
            ]
        },
        {
            "option2": null,
            "sku": "",
            "price": "1.5",
            "image_id": null,
            "requires_shipping": true,
            "compare_at_price": null,
            "option3": null,
            "id": 31900133064802,
            "taxable": true,
            "inventory_item_id": 33420786139234,
            "created_at": "2020-08-25T15:45:33-07:00",
            "product_id": 4488606810210,
            "weight": "0.0",
            "position": 3,
            "updated_at": "2020-08-25T15:45:40-07:00",
            "barcode": null,
            "inventory_policy": "deny",
            "weight_unit": "lb",
            "option1": "Variant 3",
            "admin_graphql_api_id": "gid://shopify/ProductVariant/31900133064802",
            "inventory_management": "shopify",
            "fulfillment_service": "manual",
            "grams": 0
        },
        {
            "inventory_management": "shopify",
            "inventory_policy": "deny",
            "position": 4,
            "option1": "Variant 4",
            "price": "4",
            "requires_shipping": true,
            "taxable": true,
            "image_id": null,
            "metafields": [
                {
                    "value": "1231357732959818028",
                    "key": "variant_id",
                    "value_type": "string",
                    "namespace": "shopventory"
                }
            ]
        }
    ],
    "published_scope": "global"
}

 

4. And I get back the error message:

 

{"errors":{"metafields.key":["must be unique within this namespace on this resource"]}}

 

 

As you can see, each of the variants should have just one variant_id metafield key, so I don't understand why this isn't going through. This is also a recent change, as these api calls have been working correctly for a long time (years). In addition, if instead I make the call to create each of those new variants w/ the same data, I get no error and everything goes through.

Any feedback would be much appreciated, thanks in advance!

-Rares

Replies 5 (5)
Rares_Saftoiu
Shopify Partner
14 0 3

A quick update: In the above sequence of api calls, as part of the PUT, I am also renaming the original Variant 1 to Variant 3, and creating a new variant called Variant 1. I think this is triggering the issue, because if I make the same api call, but w/out trying to create a new variant that has the same name as the original variant (e.g. I rename Variant 1 to Variant 1a and just add Variant 2, Variant 3, Variant 4) it goes through w/out issues.

Rares_Saftoiu
Shopify Partner
14 0 3

So it looks like there's likely a bug on the Shopify api.  From observing this behavior, it seems that internally they're doing some sort of name matching, matching the new Variant 1 with the old Variant 1, then thinking there's a duplicated metafield key.

Is there anyone from the Shopify team that could weigh in here?

Much appreciated,

-Rares

Rares_Saftoiu
Shopify Partner
14 0 3

This is also an issue when using the graphsql api.

joseppi
New Member
1 0 0

Also having this issue, any idea what the problem was?

Rares_Saftoiu
Shopify Partner
14 0 3

So unfortunately Shopify support wasn't super helpful, so we had to come up with a workaround where we break up our api calls so we are never deleting a variant and then adding a new variant with the same name as the deleted one. That seems to be the cause of the bug, so if you can detect you are about to do that, just break it up into 2 calls - one removing the variant, and another one adding the new variant.