Magento – Magento 2 – setup primary key field without auto increment

databasemagento2table

I want to set a field in my table database with integer type but with no auto-increment id, is this the right way to do it?

$table_lime_provinces->addColumn(
    'provinces_id',
    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
    null,
    array('identity' => true,'nullable' => false,'primary' => true,'unsigned' => true),
    'Entity ID'
);

Best Answer

You need to write this code in your Resource model class

$this->_isPkAutoIncrement = false;

for example

namespace Company\Module\Model\ResourceModel;

class Module extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
{
    protected function _construct()
    {
        $this->_init('table_name', 'table_pk_id');
        $this->_isPkAutoIncrement = false;
    }
}
Related Topic