1

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**
  • are you facing any issue or just table is not created? – Rutvee Sojitra Oct 10 '18 at 9:48
  • just table creation – Rehan Oct 10 '18 at 9:49
1

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

  • It was because of the namespace, it had nothing to do with the module.xml file. Thanks for the help. – Rehan Oct 10 '18 at 9:51
  • if your problem solved. you can accept it as an answer – Hitesh Oct 10 '18 at 9:53
0

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
0

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

sudo php bin/magento setup:upgrade

0

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

namespace Learning\GreetingMessage\Setup;

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.