cancel
Showing results for 
Search instead for 
Did you mean: 

Unique constraint violation when adding category via Rest API

Unique constraint violation when adding category via Rest API

Been trying to programmatically create the category hierarchy in a fresh Magento 2.2.3 install, but running into Unique constraint violations for some categories which I've failed to understand.

 

Example of an API request which fails:

 

POST https://xxx.com/rest/default/V1/categories

 

{"category": {"position": 21, "is_active": false, "name": "05-08 RSV1000R", "custom_attributes": [{"attribute_code": "url_key", "value": "street-bikes/wheels-and-tires/wheels/bst-carbon-fiber-wheels/aprilia-bst-wheels/rsv4/05-08-rsv1000r"}, {"attribute_code": "meta_title", "value": "Aprilia 05-08 RSV1000R Motorcycle : XXX Performance"}, {"attribute_code": "meta_keywords", "value": "bst, bst wheel, bst carbon wheel, bst carbon wheels, Black stone, Black Stone Tech"}, {"attribute_code": "meta_description", "value": "XXX Performance sells light weight carbon fiber BST wheels for your Aprilia motorcycle"}], "parent_id": 3746}}

 

Response:

 

{
    "message": "Could not save category: %1",
    "parameters": [
        "Unique constraint violation found"
    ]
}

Changing the name and values of all custom_attributes does not make a difference. What could be the unique constraint that is being violated?  Or where/how could I get more detail from Magento regarding this?  I've checked all the files in ./var/log but nothing appears in any of those log files.

 

Thank-you in advance for your help!

 

4 REPLIES 4

Re: Unique constraint violation when adding category via Rest API

Hi @david_boone

 

I have checked the REST API url posted along with its parameters - its working at my end !

 

i have use same parameter you posted and calls the rest api  - category is created successfully,

 

Then second time i have change all the parameters value and then again called the api and category is created successfully.

 

So to confirm over here - are you changing category url-key value as well ? its must needs to be change - as its required as unique !

 

You can refer swagger for more details - https://devdocs.magento.com/swagger/

if issue solved,Click Kudos & Accept as Solution

Re: Unique constraint violation when adding category via Rest API

Yes, url_key is for sure unique. Even changing it to a random string I still get the same error.

I really wish Magento would log more detail somewhere so I’d at least have a thread to follow.

Re: Unique constraint violation when adding category via Rest API

Hi @david_boone

 

I understand  - but something is still missing at your end because i have tried this in POSTMAN and its working well , i am not able to reproduce this issue !

 

Can you check in POSTMAN and then let me know is it work for you or not ? or possible share screenshot here for the same - so its help us to troubleshoot the issue

if issue solved,Click Kudos & Accept as Solution

Re: Unique constraint violation when adding category via Rest API

 

I'm not surprised it works in a blank or different copy of magento.  In fact if I just change the parent_id to `2` then it works successfully as well.  In that case if I run it again I get a clear error regarding the url_key already existing.  Changing url_key and it works again, giving me two identical (except for url_key) categories.  So clearly the name or meta attributes can't be the issue since Magento has no problem with any of those being duplicated.  The position in the category hierarchy seems to matter, but a previous category *was* created with the same parent_id.

 

So I'm left with 20% of my categories fail to add, and with no explanation why.  Is there any way to enable more verbose error messages?  Just knowing which unique constraint is being violated would be a huge help.

 

BTW, the issue first appeared during a run of our script which was calling the API, then I used Postman to reproduce and attempt to debug.

postman.png