1

I have an InstallSchema.php that does not create the table in the database. Here is the code of the schema php:

<?php

namespace MyVendor\News\Setup;

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

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

        // Get news table
        $tableName = $installer->getTable('simplenews');
        // Check if the table already exists
        if ($installer->getConnection()->isTableExists($tableName) != true) {
            // Create news 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);
        }

        $installer->endSetup();
    }
}

I know I'm not the first one to have this issue, so I tried everything I could find about this. Here is what I have done to try make it works.

Before every try, executing sql

Delete from setup_module where module like '%module_name%'; 

I have of course checked that the query worked properly.

I have tried to install it with UpgradeSchema.php and change the version of the module in

magento/app/code/MyVendor/module/etc/module.xml (from 1.0.0 to 1.0.1), 

it didn't work.

I have also tried to install it with Recurring.php

I'm using the command

sudo php bin/magento setup:upgrade

to install the table in the database. This command display no error and i can see my module in the output of the command.

I have also tried

setup:di:compile 

to see any errors, but can't find any errors.

I had no problem installing magento, everything looks good so I'm lost.

EDIT : My module is active (I checked it with sudo php bin/magento module:status)

  • you can i also delete your table from database – Ranjeet Kumar Feb 25 at 13:29
  • you mean delete the setup_module table from my database? – ChristopheS Feb 25 at 13:43
  • @ChristopheS you made this mistake : 'primary' => true, you forget to put comma after true – Chirag Parmar Feb 25 at 13:45
  • simplenews table remove from database. – Ranjeet Kumar Feb 25 at 14:18
  • The problem is that the table is not created by the script at all, this is what I'm trying to figure it out – ChristopheS Feb 25 at 14:47
0
<?php

namespace VendoreName\ModuleName\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();

        // Get news table
        $tableName = $installer->getTable('simplenews');
        // Check if the table already exists
        if ($installer->getConnection()->isTableExists($tableName) != true) {
            // Create news 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);
        }

        $installer->endSetup();
    }
}

your code is running perfectly i used and table is created.

enter image description here

Please make you sure you run magento command at right place and delete row from setup_module table and also check your table is exist or not. code is working.

Please check which database are you used ??

After php bin/magento setup:upgrade run this command you can see your module name ?? ( Terminal/putty )

Check you run your running magento not Other Magentos ??

Run php bin/magento setup:upgrade command before delete row from Setup_module table.

Run php bin/magento module:s

You can check your module status and if it is disable then run this command

php bin/magento module:e Modulename_VendoreName 

And again run

php bin/magento setup:upgrade
| improve this answer | |
  • I have no idea what to do, I'm 100% sure I'm removing the right row from setup_module, I'm using the command from my magento root directory – ChristopheS Feb 25 at 13:37
  • @ChristopheS answer is updat please check and let me inform your table is created or not ?? – Msquare Feb 25 at 14:20
  • I have only one magento, I'm using the database "magento", this is a fresh installation of magento, I can see my module name after running bin/magento setup:upgrade. What I do before upgrading is 1) checking if the table was created or not 2) if not remove my module from setup_module 3) run the command bin/magento setup:upgrade – ChristopheS Feb 25 at 14:50
  • Have you check Module is Enable or not ?? Using above command which mension in above answer. – Msquare Feb 25 at 15:00
  • Yes I have checked it's active – ChristopheS Feb 25 at 15:01
0

I have found what was wrong, my folders had a lowercase name, so in my install schema neither with

namespace Vendor\News\Setup;

or with

namespace vendor\news\setup;

the install function were called when i was doing the upgrade command. I just had to replace the first letter of my folders with uppercase and then change my namespace to

namespace Vendor\News\Setup;

Now, everything works perfectly

| improve this answer | |

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.