1

I am trying to make the length higher than 255

<?php

namespace []\[]\Setup;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;

class InstallSchema implements InstallSchemaInterface
{
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();

        if (version_compare($context->getVersion(), '0.0.1') < 0) {

            $tableName = $installer->getTable('review_comment');
            if ($installer->getConnection()->isTableExists($tableName) != true) {

                $table = $installer->getConnection()
                    ->newTable($tableName)
                    ->addColumn(
                        'id',
                        Table::TYPE_INTEGER,
                        null,
                        array(
                            'identity' => true,
                            'nullable' => false,
                            'primary' => true,
                        ),
                        'ID'
                    )

                    ->addColumn('comment', Table::TYPE_TEXT, '2M', [], 'Comment') //NOW this fixed don't know how 

                    ->addColumn(
                        'enable',
                        Table::TYPE_INTEGER, null,
                        array(
                            'nullable' => false,
                        ),
                        'Enable'
                    )
                    ->addColumn(
                        'creator',
                        Table::TYPE_TEXT, 255,
                        array(
                            'nullable' => false,
                        ),
                        'Creator'
                    )
                    ->addColumn(
                        'created_at',
                        Table::TYPE_TIMESTAMP,
                        null,
                        ['nullable' => true],
                        'Created At'
                    )
                    ->addColumn(
                        'updated_at',
                        Table::TYPE_TIMESTAMP,
                        null,
                        ['nullable' => true],
                        'Updated At'
                    )
                    ->setComment('Custom');

                $installer->getConnection()->createTable($table);
            }
        }
        $installer->endSetup();
    }
}

but I get the following error -> Syntax error or access violation: 1170 BLOB/TEXT column 'comment' used in key specification without a key length

But this didn't work

->addColumn(
    'comment',
    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
    '2M',
    ['nullable' => false],
    'Comment'
)
2

try this

->addColumn(
    'your_column',
    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
    '2M',
    ['nullable' => false, 'default' => '0'],
    'Comment'
)

Use declarative schema for creating a table with Column

https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/db-schema.html

If already created InstallSchema.php in your module then create UpgradeSchema.php for creating and updating a new column in table

Remove Module entry in the setup_module table and also remove the review_comment table in the database and hit upgrade command and check.

InstallSchema.php

<?php

namespace [Vendor]\[Module]\Setup;

use Magento\Framework\Setup\InstallSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\DB\Ddl\Table;

/**
 * Class InstallSchema
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @throws \Zend_Db_Exception
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
        $installer->startSetup();
        $table = $installer->getConnection()->newTable(
            $installer->getTable('review_comment')
        )->addColumn(
            'id',
            Table::TYPE_INTEGER,
            null,
            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
            'ID'
        )->addColumn(
            'comment',
            Table::TYPE_TEXT,
            '2M',
            ['nullable' => false],
            'Comment'
        )->addColumn(
            'enable',
            Table::TYPE_INTEGER,
            null,
            ['nullable' => false],
            'Enable'
        )->addColumn(
            'creator',
            Table::TYPE_TEXT,
            255,
            ['nullable' => false],
            'Creator'
        )->addColumn(
            'created_at',
            Table::TYPE_TIMESTAMP,
            null,
            ['nullable' => false, 'default' => Table::TIMESTAMP_INIT],
            'Created At'
        )->addColumn(
            'updated_at',
            Table::TYPE_TIMESTAMP,
            null,
            ['nullable' => false, 'default' => Table::TIMESTAMP_INIT],
            'Updated At'
        )->setComment('Custom');
        $installer->getConnection()->createTable($table);

        $installer->endSetup();
    }
}

I hope this will help you...!

| improve this answer | |
  • When i run set:up i get error new error Syntax error or access violation: 1101 BLOB, TEXT, GEOMETRY or JSON column 'comment' can't have a default value – jibin george Jun 3 at 10:51
  • Have you try above code – Devidas Jun 3 at 11:26
  • yeah that didn't work for me but this worked don't know why ->addColumn('comment', Table::TYPE_TEXT, '2M', [], 'Comment') – jibin george Jun 3 at 11:34
  • Can you show me your schema file ? Please upload in question so will try to solve your problem – Devidas Jun 3 at 11:39
  • 1
    hey check I have updated my answer please check and follow above setps. I hope this will help you. – Devidas Jun 3 at 13:34

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.