Question

I am trying to create a new table via my module Ved_Mymodule

This module is active and when I change its setup_version in module.xml file then it is reflecting in setup_module table in the database.

\app\code\Ved\Mymodule\Setup\UpdateSchema.php

<?php

namespace Ved\Mymodule\Setup;

use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $installer = $setup;
    $installer->startSetup();

    // Get emqube_mymodule table
    $tableName = $installer->getTable('ved_mymodule');
    // Check if the table already exists
    if ($installer->getConnection()->isTableExists($tableName) != true) {
        // Create emqube_mymodule table
        $table = $installer->getConnection()
            ->newTable($tableName)
            ->addColumn(
                'id',
                Table::TYPE_INTEGER,
                null,
                [
                    'identity' => true,
                    'unsigned' => true,
                    'nullable' => false,
                    'primary' => true
                ],
                'ID'
            )
            ->addColumn(
                'title',
                Table::TYPE_TEXT,
                null,
                ['nullable' => false, 'default' => ''],
                'Title'
            )
            ->addColumn(
                'summary',
                Table::TYPE_TEXT,
                null,
                ['nullable' => false, 'default' => ''],
                'Summary'
            )
            ->addColumn(
                'description',
                Table::TYPE_TEXT,
                null,
                ['nullable' => false, 'default' => ''],
                'Description'
            )
            ->addColumn(
                'created_at',
                Table::TYPE_DATETIME,
                null,
                ['nullable' => false],
                'Created At'
            )
            ->addColumn(
                'status',
                Table::TYPE_SMALLINT,
                null,
                ['nullable' => false, 'default' => '0'],
                'Status'
            )
            ->setComment('News Table')
            ->setOption('type', 'InnoDB')
            ->setOption('charset', 'utf8');
        $installer->getConnection()->createTable($table);

        }

        //if (version_compare($context->getVersion(), '1.0.3') < 0) {
            //code to upgrade to 1.0.3
        //}

        $installer->endSetup();
    }
}

Then I have run:

php bin/magento setup:db-schema:upgrade

php bin/magento setup:upgrade

But that table is not creating in the database.

Was it helpful?

Solution

Try below steps.

  1. Go to current Magento instances Database and remove the record from setup_module with module = 'Ved_Mymodule'.
  2. Remove the cache.
  3. Run the command php bin/magento setup:upgrade.

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top