Search code examples
modelmagento2magento-2.0

Magento 2 auto create custom table from custom module


I just upgraded from Magento 1.7. I successfully created module, controllers, helpers etc.

Today I tried to create a model with custom table. I want the table to be auto-created. I did following steps:

I created a module with following directory structure:

<Ashutosh>
  <Pandey>
      <etc>
          module.xml
      <Model>
      <Setup>
          InstallSchema.php

Here are the contents of each file:

InstallSchema.php

<?php

namespace Ashutosh\Pandey\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 table
        $tableName = $installer->getTable('ashutosh_friends');

        // Check if the table already exists
        if ($installer->getConnection()->isTableExists($tableName) != true) {
            // Create tutorial_simplenews table
            $table = $installer->getConnection()
                ->newTable($tableName)
                ->addColumn(
                    'id',
                    Table::TYPE_INTEGER,
                    null,
                    [
                        'identity' => true,
                        'unsigned' => true,
                        'nullable' => false,
                        'primary' => true
                    ],
                    'ID'
                )
                ->addColumn(
                    'name',
                    Table::TYPE_TEXT,
                    null,
                    ['nullable' => false, 'default' => ''],
                    'Title'
                )
                ->addColumn(
                    'friend_type',
                    Table::TYPE_TEXT,
                    null,
                    ['nullable' => false, 'default' => ''],
                    'Summary'
                )
                ->addColumn(
                    'created_at',
                    Table::TYPE_DATETIME,
                    null,
                    ['nullable' => false],
                    'Created At'
                )
                ->addColumn(
                    'status',
                    Table::TYPE_SMALLINT,
                    null,
                    ['nullable' => false, 'default' => '0'],
                    'Status'
                )
                ->setComment('Ashutosh Friends Table')
                ->setOption('type', 'InnoDB')
                ->setOption('charset', 'utf8');
            $installer->getConnection()->createTable($table);
        }

        $installer->endSetup();
    }
}

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="Ashutosh_Pandey" setup_version="1.0.0" active="true"/>
</config>

Then on command prompt from my magento2 directory, I executed the command:

php bin/magento setup:upgrade

The command runs successfully but the table is not getting created. I then also executed the command:

php bin/magento setup:db-schema:upgrade

but still nothing happen.

Note: I can read table successfully if I create it manually in database.


Solution

  • Magento matches the module version specified in module.xml against entries in the database table setup_module. You could try to remove the entry of your custom module which should trigger magento to run the install script again when using php bin/magento setup:upgrade

    select * from core_config_data;
    
    delete from setup_module where module="<YOURMODULE>";