2

I have created the basic model. But the custom table does not add in the database.

My Magento V2.0

1 Answer 1

5

Inside your custom module add Setup/InstallSchema.php file

namespace Vendor\Module\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
    ) {
        $setup->startSetup();
        $context->getVersion();

        $table = $setup->getConnection()->newTable(
            $setup->getTable('tablename')
        )->addColumn(
            'id',
            Table::TYPE_INTEGER,
            null,
            [
                'identity' => true,
                'unsigned' => true,
                'nullable' => false,
                'primary' => true],
            'ID'
        )->addColumn(
            'name',
            Table::TYPE_TEXT,
            255,
            [],
            'Name'
        )->addColumn(
            'firstname',
            Table::TYPE_TEXT,
            255,
            [],
            'FirstName'
        )->addColumn(
            'created_at',
            Table::TYPE_TIMESTAMP,
            null,
            [
                'nullable' => false,
                'default' => Table::TIMESTAMP_INIT],
            'Created date'
        )->addColumn(
            'updated_at',
            Table::TYPE_TIMESTAMP,
            null,
            [
                'nullable' => false,
                'default' => Table::TIMESTAMP_INIT],
            'Updated date'
        )->addIndex(
            $setup->getIdxName('tablename', ['id']),
            ['id']
        )->setComment(
            'table related comments'
        );
        $setup->getConnection()->createTable($table);
        $setup->endSetup();
    }
}

Than enable module using

bin/magento module:enable Vendor_Module and

bin/magento setup:upgrade

NOTE : if you have added the table creation script after the module is already enabled than try removing the entry of the module from setup_module table and than run bin/magento module:enable Vendor_Module and bin/magento setup:upgrade commands or try creating table using UpgradeSchema script.

Hope this helps

9
  • I have followed the same step but table not created after ran upgrade command. Sep 24, 2018 at 9:50
  • have you added the table creation script after the module is enabled ? Please check the entry inside setup_module table and delete the entry for your custom module and try executing the commands for module enable and setup:upgrade
    – Mac
    Sep 24, 2018 at 9:52
  • Module already enabled. Sep 24, 2018 at 9:53
  • module created successfuly but table does not add in database. Sep 24, 2018 at 9:54
  • please post your code .
    – Mac
    Sep 24, 2018 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.