I have a table which store certain data in database. First i am checking if the content is already there in the database if yes then update it else insert a new data.

$sync = $this->inventoryCollection->create()
        ->addFieldToSelect("*")->addFieldToFilter("product_id", ["eq"=>$productId])
        ->addFieldToFilter("type", ["eq"=>$type])->getFirstItem();
    $totQty = $qty;
    $id = 0;
        $invent = $this->inventorySync->create()->load($sync->getData("inventory_track_id"),"inventory_track_id");
            $invent->setData("qty", $qty);
        else {
            $totQty = $invent->getData("qty") + $qty;
            $invent->setData("inventory_track_id", $sync->getData("inventory_track_id"));
            $invent->setData("qty", $totQty);
            $invent->setData("product_id", $productId);
            $invent->setData("sync", true);
        $id = $invent->getId();
    else {
        $invent = $this->inventorySync->create();
            "product_id" => $productId,
            "qty" => $qty,
            "type" => $type,
            "sync" => true
        $id = $invent->getId();

The strange thing is i am getting an error saying "Unique constraint violation found"

How can i update the data?

  • please share the schema of this model – Amit Bera Feb 7 at 7:49

Found the issue.

I had made a small mistake at Resource Class.

protected function _construct()
    $this->_init('avesh_inventory_qty_track', ' inventory_track_id');

It should had been

protected function _construct()
    $this->_init('avesh_inventory_qty_track', 'inventory_track_id');

No Spaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.