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
.* FROMtivol_storeavailability
ASmain_table
Any ideas as to why the table is not being created
$installer->getTable('tivol_storeavailability')
inside ofisTableExist
, try changing$tableName
to$tableName = 'tivol_storeavailability';