Question

I have been trying to create a table from InstallSchema.php, but some reason the table is getting created. I haved created a bare minimum module with the following files.

1. registration.php

    <?php
    /**
     * Copyright © Magento, Inc. All rights reserved.
     * See COPYING.txt for license details.
     */

    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Rehan_Test',
        __DIR__
    );

2. etc/module.xml

    <?xml version="1.0"?>

    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
       <module name="Rehan_Test" setup_version="1.0"/>
     </config>

3. Setup/InstallSchema.php

    <?php
    /**
     * Copyright © 2016 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */

    namespace Learning\GreetingMessage\Setup;
    use Magento\Framework\Setup\InstallSchemaInterface;
    use Magento\Framework\Setup\ModuleContextInterface;
    use Magento\Framework\Setup\SchemaSetupInterface;

    /**
     * @codeCoverageIgnore
     */
    class InstallSchema implements InstallSchemaInterface
    {
        /**
         * {@inheritdoc}
         * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
         */
        public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
        {
            /**
             * Create table 'greeting_message'
             */
            $table = $setup->getConnection()
                ->newTable($setup->getTable('greeting_message'))
                ->addColumn(
                    'greeting_id',
                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    null,
                    ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                    'Greeting ID'
                )
                ->addColumn(
                    'message',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    255,
                    ['nullable' => false, 'default' => ''],
                    'Message'
                )->setComment("Greeting Message table");
            $setup->getConnection()->createTable($table);
        }
    }

    ?>

After all this, I try to run the upgrade command like so

**sudo php bin/magento setup:upgrade**
Was it helpful?

Solution

correct your module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Rehan_Test" setup_version="1.0.0"></module>
</config>

seems Setup/InstallSchema.php file have the wrong namespace it should be

namespace Rehan\Test\Setup;

get reference from here

OTHER TIPS

Follow below step to create custom table :

Replace this below code in your module.xml :

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Learning_GreetingMessage" schema_version="0.0.1" setup_version="0.0.1">
    </module>
</config>

Replace this below code in your InstallSchema.php file :

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

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

/**
 * @codeCoverageIgnore
 */
class InstallSchema implements InstallSchemaInterface
{
    /**
     * {@inheritdoc}
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     */
    public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        /**
         * Create table 'greeting_message'
         */
        $installer = $setup;
        $installer->startSetup();
        $tableName = $installer->getTable('greeting_message');
        if ($installer->getConnection()->isTableExists($tableName) != true) {
            $table = $installer->getConnection()
                ->newTable($setup->getTable('greeting_message'))
                ->addColumn(
                    'greeting_id',
                    \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                    null,
                    ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                    'Greeting ID'
                )
                ->addColumn(
                    'message',
                    \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    255,
                    ['nullable' => false, 'default' => ''],
                    'Message'
                )->setComment("Greeting Message table");
            $setup->getConnection()->createTable($table);
        }
        $installer->endSetup();
    }
}

?>

Go to setup_module table and check Learning_GreetingMessage module entry exist or not. If exist then remove it and remove your table.

Then, execute this below command.

php bin/magento setup:upgrade

php bin/magento cache:flush

delete your module from database . table "setup_module" delete column "Rehan_Test" and run

sudo php bin/magento setup:upgrade

You have used wrong Namespace "Learning\GreetingMessage" in InstallSchema.php file. Please correct it.

namespace Learning\GreetingMessage\Setup;

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