1

I would like to add a new column inside the magento2 database. Write now i add a \Magento\Framework\DB\Ddl\Table::TYPE_TEXT as the column type. here is my code

    if(!$installer->getConnection()->tableColumnExists('review_detail', 'email')) {
        $definition = [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            'size' => 255,
            ['nullable' => true, 'length' => 255],
            'comment' => 'User Email'
        ];
        $installer->getConnection()->addColumn(
            'review_detail',
            'email',
            $definition
        );
    }

right now, when i do my php bin/magento setup:upgrade the new column are set as text(65535).

I'm using MySQL as database. How i can set the new column as VARCHAR(255)? i've looked on several website and everything i see is to use TYPE_TEXT with a size under 255 to setup a VARCHAR.

1

Try using it like,

if(!$installer->getConnection()->tableColumnExists('review_detail', 'email')) {
        $definition = [
            'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
            'size' => 255,
            'nullable' => true,
            'length' => 255,
            'comment' => 'User Email'
        ];
        $installer->getConnection()->addColumn(
            'review_detail',
            'email',
            $definition
        );
    }
  • Thank you, that's worked!! what's the différence between ['nullable' => true, 'length' => 255] and your way? – Stéphane Baribeau May 30 at 13:40
  • @StéphaneBaribeau, With your code, it was creating a sub array withing definition array, but if you check the definition of addColumn function, it gets value from definition array and not from its sub array. So, you need to add it as your definition array element and not its sub array. – Jaimin Sutariya May 31 at 2:33
  • ohh i see!! thanks! – Stéphane Baribeau May 31 at 18:30

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.