2

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'
);
5

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;
    }
}
| improve this answer | |
  • @Rakesh whai if i turn the 'identity' => false in add column, is it gonna disable the auto_increment too? – Idham Choudry Jan 17 '17 at 6:39
  • @IdhamChoudry any update on your question ? 'identity' => false in add column disables the auto_increment or not ? – Ajwad Syed May 5 '19 at 9:13
1

You can just start using,

Using identify to false, prevent auto increment for your table.

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

First set

$this->_isPkAutoIncrement = false;

Eg

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;
    }
}

Then in your setup script

->addColumn(
     'table_pk_id',
     \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
     32,
     ['nullable' => false, 'primary' => true],
     'Status'
)

Since identity is default false lib/internal/Magento/Framework/DB/Ddl/Table.php

$identity = false;
....
if (!empty($options['identity']) || !empty($options['auto_increment'])) {
    $identity = true;
}
| improve this answer | |
0

I have yet to figure out why this happens. It's either something I'm just missing, but I've stepped through it 100 times, I've setup the InstallSchema sequence to set 'identity to false, I've deleted all generated code and nothing but setting this class variable to false works. I've got to think this is a huge bug in the Magento code, but I always hate to blame anything but myself for silly, blatant crap like this. So... Hack in place. Moving on.

| improve this answer | |

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.