1

I'm trying to add a custom table for a module. The following code sits the Setup folder of my module:

<?php

namespace tivol\storeAvailability\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 tivol_storeavailability table
    $tableName = $installer->getTable('tivol_storeavailability');
    // Check if the table already exists
    if ($installer->getConnection()->isTableExists($tableName) != true) {
        // Create tivol_storeavailability table
        $table = $installer->getConnection()
            ->newTable($tableName)
                        ->addColumn(
                            'id',
                            \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
                            null,
                            ['identity' => true, 'unsigned' => true, 'nullable' => false, 'primary' => true],
                            'Id'
                        )
                        ->addColumn(
                            'label',
                            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                            null,
                            ['default' => null, 'nullable' => false],
                            'Name'
                        )
                        ->addColumn(
                            'value',
                            \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                            null,
                            ['default' => null, 'nullable' => false],
                            'Stores'
                        );
            ->setComment('News Table')
            ->setOption('type', 'InnoDB')
            ->setOption('charset', 'utf8');
        $installer->getConnection()->createTable($table);
    }

    $installer->endSetup();
}
}

when I run it I get the following error on the frontend:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'magento.tivol_storeavailability' doesn't exist, query was: SELECT main_table.* FROM tivol_storeavailability AS main_table

Any ideas as to why the table is not being created

2
  • you have to first delete your module entry from setup_module table and try again. Commented Aug 22, 2016 at 9:02
  • you are inserting $installer->getTable('tivol_storeavailability') inside of isTableExist, try changing $tableName to $tableName = 'tivol_storeavailability';
    – belfort1
    Commented Aug 22, 2016 at 9:02

0

Your Answer

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

Browse other questions tagged or ask your own question.