Hi,
I have created a custom module but the table is not created.
My code is below
Do you have any suggestions to find out what went wrong?
<?php namespace Mastering\SampleModule\Setup; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\SchemaSetupInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\DB\Ddl\Table; class InstallSchema implements InstallSchemaInterface { public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); $table= $setup->getConnection()->newTable( $setup->getTable("mg_mastering_sample_item") )->addColumn( 'id', Table::TYPE_INTEGER, null, ['identity'=>true, 'nullable'=>false, 'primary'=>true], 'Item ID' )->addColumn( 'name', Table::TYPE_TEXT, 255, ['nullable' => false], 'Item Name' )->addIndex( $setup->getIdxName('mg_mastering_sample_item', ['name']), ['name'] )->setComment( 'Sample Items' ); $setup->getConnection()->createTable($table); $setup->endSetup(); }
Hello
It seems like you have already used bin/magento setup:upgrade before writing create table code.
So, either you need to use UpgradeSchema.php to create table or you need to remove entry from setup_module table.
To remove entry from database check below mentioned steps:
1. You can search your module entry "Mastering_SampleModule" in setup_module table
2 Remove the entry with module name "Mastering_SampleModule" from table.
3. Run command bin/magento setup:upgrade
If Issue Solved, Click Kudos/Accept As solutions.
Hi @tvgarden
Might be your module entry is there in setup_module table.
So remove that entry and replace below code with your existing code and then check
Try below code :
<?php namespace Mastering\SampleModule\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 { /** * Installs DB schema for a module * * @param SchemaSetupInterface $setup * @param ModuleContextInterface $context * @return void */ public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $table = $installer->getConnection() ->newTable($installer->getTable('mg_mastering_sample_item')) ->addColumn( 'id', Table::TYPE_INTEGER, null, ['identity' => true, 'nullable' => false, 'primary' => true], 'Item ID' )->addColumn( 'name', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 255, ['nullable' => false], 'Item Name' )->addIndex( $setup->getIdxName('mg_mastering_sample_item', ['name']), ['name'] )->setComment('Sample Items'); $installer->getConnection()->createTable($table); $installer->endSetup(); } }
Hi @Hitarth Pattani @Manthan Dave
I tried removing the entry and re run the command, but no table created.
I used upgradeSchema.php but didn't work:
<?php namespace Test\TestAgain\Setup; use Magento\Framework\Setup\UpgradeSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; class UpgradeSchema implements UpgradeSchemaInterface { /** * {@inheritdoc} */ public function upgrade( SchemaSetupInterface $setup, ModuleContextInterface $context ) { $installer = $setup; $installer->startSetup(); $table = $installer->getConnection() ->newTable($installer->getTable('mg_mastering_sample_item')) ->addColumn( 'id', Table::TYPE_INTEGER, null, ['identity' => true, 'nullable' => false, 'primary' => true], 'Item ID' )->addColumn( 'name', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 255, ['nullable' => false], 'Item Name' )->addIndex( $setup->getIdxName('mg_mastering_sample_item', ['name']), ['name'] )->setComment('Sample Items'); $installer->getConnection()->createTable($table); $installer->endSetup(); } }
Hello @tvgarden
If you remove entry from database and run the upgrade command then you do not need to transfer your code to UpgradeSchema.php. Remove entry from database only requires when you use install schema.
To run upgrade schema file, you need to use version compare code to install table. Please change the setup_version attribute in module.xml and place below code in your upgrade function:
<?php namespace Test\TestAgain\Setup; use Magento\Framework\Setup\UpgradeSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; class UpgradeSchema implements UpgradeSchemaInterface { /** * {@inheritdoc} */ public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context) { if (version_compare($context->getVersion(), '[YOUR UPGRADED VERSION]', '<')) { $installer = $setup; $installer->startSetup(); $table = $installer->getConnection() ->newTable($installer->getTable('mg_mastering_sample_item')) ->addColumn( 'id', Table::TYPE_INTEGER, null, ['identity' => true, 'nullable' => false, 'primary' => true], 'Item ID' )->addColumn( 'name', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 255, ['nullable' => false], 'Item Name' )->addIndex( $setup->getIdxName('mg_mastering_sample_item', ['name']), ['name'] )->setComment('Sample Items'); $installer->getConnection()->createTable($table); $installer->endSetup(); } } }
In above code, just replaced the version defined in module.xml as an attribute setup_version with [YOUR UPGRADED VERSION].
If Issue Solved, Click Kudos/Accept As solutions.
I did them separately.
1. I removed the entry and ran the code -> nothing happened
2. I added upgradeSchema.php and ran the code -> nothing happened
First you need to remove entry from setup_module table.
Delete entry with Mastering_SampleModule from setup_module table from database.
Keep below code in your Installschema.php file,
<?php namespace Mastering\SampleModule\Setup; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\SchemaSetupInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\DB\Ddl\Table; class InstallSchema implements InstallSchemaInterface { public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $table = $installer->getConnection() ->newTable($installer->getTable('mg_mastering_sample_item')) ->addColumn( 'id', Table::TYPE_INTEGER, null, ['identity'=>true, 'nullable'=>false, 'primary'=>true], 'Item ID' ) ->addColumn( 'name', Table::TYPE_TEXT, 255, ['nullable' => false], 'Item Name' )->addIndex( $setup->getIdxName('mg_mastering_sample_item', ['name']), ['name'] )->setComment( 'Sample Items' ); $installer->getConnection()->createTable($table); } }
Run below command,
php magento setup:upgrade php magento setup:static-content:deploy -f
Please keep install schema as it is and remove the entry from the table.
That will work.
No need to apply both things.