How to Set Custom Value to Identity Column of a Table in Magento 2

columnmagento-2.1model

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?

Best Answer

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