2

I have a custom table with identity column id and text column otherColumn and I'm trying to add a row into the table with my custom value.

$model->setOtherColumn($otherColumnValue)->save();

Above code works and inserts a row with next occurring value in "id" column. But when I use below code.

$model->setId($customValue)->setOtherColumn($otherColumnValue)->save();

Row is not inserted in the table.

How to insert custom value in identity column?

1 Answer 1

2

When you provide primary_key While inserting a row, Magento assumes it as an Update operation and it fails because no such identity exist in Database.

However if you still want to insert a row with specified Primary Key then you can turn off the constraint by adding following line to class of resource model of that respective table.

protected $_isPkAutoIncrement = false;

For detailed information, you can refer to the solution provided for Magento version 1.x

Model save using setData and addData not adding to database

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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