Hello
I'm a french student and I'm actually trying to learn how to use Magento but I'm stuck since yesterday i can't create a Table in the setup of my module tried everything pls help (sry for bad english).
here is the code :
<?php namespace MonModule\AdminPage\Setup; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; class InstallSchema implements InstallSchemaInterface { public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); if (!$setup->getConnection()->isTableExists($setup->getTable('table-i-want-to-create'))) { $table = $setup->getConnection() ->newTable($setup->getTable('table-i-want-to-create')) ->addColumn('id', Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11,['auto_increment' => true, 'nullable' => false, 'primary' => true], 'ID') ->addColumn('id_shop', Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11, ['nullable' => false, 'default' => ''], 'Title') ->addColumn('name', Magento\Framework\DB\Ddl\Table::TYPE_VARCHAR, 64, ['nullable' => false, 'default' => ''], 'Name') ->addColumn('shipping', Magento\Framework\DB\Ddl\Table::TYPE_DOUBLE, (20,6), ['nullable' => false, 'default' => ''], 'Shipping') ->addColumn('warranty', Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11, ['nullable' => false], 'Warranty') ->addColumn('full_attributes', Magento\Framework\DB\Ddl\Table::TYPE_TINYINT, 1, ['nullable' => false, 'default' => ''], 'Full_attributes') ->setComment('News Table') ->setOption('type', 'InnoDB') ->setOption('charset', 'utf8'); $setup->getConnection()->createTable($table); } $setup->endSetup(); } }
pls tell me if you see something wrong
Solved! Go to Solution.
Hello @Antoine553,
Go to your db, then find module_setup table then remove the entry of your module.
Then refresh cache and run setup upgrade command.
If works then mark as solution.
You have to keepBelow code in your InstallSchema .php file and run upgrade command,
php bin/magento setup:upgrade
<?php namespace MonModule\AdminPage\Setup; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; class InstallSchema implements InstallSchemaInterface { public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $table = $installer->getConnection() ->newTable($installer->getTable('table-i-want-to-create')) ->addColumn('id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11,['auto_increment' => true, 'nullable' => false, 'primary' => true], 'ID') ->addColumn('id_shop', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11, ['nullable' => false], 'Title') ->addColumn('name', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 64, ['nullable' => false, 'default' => ''], 'Name') ->addColumn('shipping', \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL, '12,4', ['nullable' => false], 'Shipping') ->addColumn('warranty', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11, ['nullable' => false], 'Warranty') ->addColumn('full_attributes', \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT, 1, ['nullable' => false], 'Full_attributes') ->setComment('News Table') ->setOption('type', 'InnoDB') ->setOption('charset', 'utf8'); $installer->getConnection()->createTable($table); $installer->endSetup(); } }
In your code so many bugs like you have defined Varchar and double, whose value are not taken in magento 2 you can use alternate value decimal and text data type for those.
Second error was you have keep field as not null and set default value as null.
ex. ['nullable' => false, 'default' => ''] this is not valid.
Please let me know if you have query.
If issue solved, click Kudos and Accept as Solution.
thanks for your answer i modified my code like this but it's still not good after i execute the upgrade command it's just like it didn't even read the code:
<?php namespace MonModule\AdminPage\Setup; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; class InstallSchema implements InstallSchemaInterface { public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $setup->startSetup(); if (!$setup->getConnection()->isTableExists($setup->getTable('table-i-want-to-create'))) { $table = $setup->getConnection() ->newTable($setup->getTable('table-i-want-to-create')) ->addColumn('id', Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11,['auto_increment' => true, 'nullable' => false, 'primary' => true], 'ID') ->addColumn('id_shop', Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11, ['nullable' => false], 'Title') ->addColumn('name', Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 64, ['nullable' => false], 'Name') ->addColumn('shipping', Magento\Framework\DB\Ddl\Table::TYPE_FLOAT, (20,6), ['nullable' => false], 'Shipping') ->addColumn('warranty', Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11, ['nullable' => false], 'Warranty') ->addColumn('full_attributes', Magento\Framework\DB\Ddl\Table::TYPE_TINYINT, 1, ['nullable' => false], 'Full_attributes') ->setComment('News Table') ->setOption('type', 'InnoDB') ->setOption('charset', 'utf8'); $setup->getConnection()->createTable($table); } $setup->endSetup(); } }
You have to keep my code which is defined above answer,
<?php namespace MonModule\AdminPage\Setup; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; class InstallSchema implements InstallSchemaInterface { public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); $table = $installer->getConnection() ->newTable($installer->getTable('table-i-want-to-create')) ->addColumn('id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11,['auto_increment' => true, 'nullable' => false, 'primary' => true], 'ID') ->addColumn('id_shop', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11, ['nullable' => false], 'Title') ->addColumn('name', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 64, ['nullable' => false, 'default' => ''], 'Name') ->addColumn('shipping', \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL, '12,4', ['nullable' => false], 'Shipping') ->addColumn('warranty', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11, ['nullable' => false], 'Warranty') ->addColumn('full_attributes', \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT, 1, ['nullable' => false], 'Full_attributes') ->setComment('News Table') ->setOption('type', 'InnoDB') ->setOption('charset', 'utf8'); $installer->getConnection()->createTable($table); $installer->endSetup(); } }
In your above code,
Magento\Framework\DB\Ddl\Table::TYPE_FLOAT
and
Magento\Framework\DB\Ddl\Table::TYPE_TINYINT
which is not valid.
Use
$installer = $setup
everywher instead of $setup.
If issue solved, click Kudos and Accept as Solution.
thanks you for your help I tried with your correction but unfortunatly after the setup:upgrade command there is still no change or even error message
here is the actual code (i tried without the if but still the same result) :
<?php namespace MonModule\AdminPage\Setup; use Magento\Framework\Setup\InstallSchemaInterface; use Magento\Framework\Setup\ModuleContextInterface; use Magento\Framework\Setup\SchemaSetupInterface; class InstallSchema implements InstallSchemaInterface { public function install(SchemaSetupInterface $setup, ModuleContextInterface $context) { $installer = $setup; $installer->startSetup(); if ($installer->getConnection()->isTableExists($installer->getTable('table-i-want-to-create'))) { $table = $installer->getConnection() ->newTable($installer->getTable('table-i-want-to-create')) ->addColumn('id', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11,['auto_increment' => true, 'nullable' => false, 'primary' => true], 'ID') ->addColumn('id_shop', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11, ['nullable' => false], 'Title') ->addColumn('name', \Magento\Framework\DB\Ddl\Table::TYPE_TEXT, 64, ['nullable' => false, 'default' => ''], 'Name') ->addColumn('shipping', \Magento\Framework\DB\Ddl\Table::TYPE_DECIMAL, '12,4', ['nullable' => false], 'Shipping') ->addColumn('warranty', \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER, 11, ['nullable' => false], 'Warranty') ->addColumn('full_attributes', \Magento\Framework\DB\Ddl\Table::TYPE_SMALLINT, 1, ['nullable' => false], 'Full_attributes') ->setComment('News Table') ->setOption('type', 'InnoDB') ->setOption('charset', 'utf8'); $installer->getConnection()->createTable($table); } $installer->endSetup(); } }
Its problem of entry generated in setup_module table So first you have to remove your module entry from this table and run above script again.
Or you can use UpgradeSchema.php file and update your module version and run again this command.
If issue solved, click Kudos and Accept as Solution.
i tried the two solution but still nothing it just took a little more time to execute the commande
Hello @Antoine553,
Go to your db, then find module_setup table then remove the entry of your module.
Then refresh cache and run setup upgrade command.
If works then mark as solution.
It worked thank you very much