3

I have created a new module and installed it, but did not include Setup/InstallSchema.php to create the table first time.

Once the module is created can we create new table using InstallSchema.php?

I tried that, but no table is created. After that I used Upgradeschema.php to try to create the table by changing the version in etc/module.xml.

However, I am getting base table not found error.

I am using below code to create table in Setup/UpgradeSchema.php

  use Magento\Framework\Setup\UpgradeSchemaInterface;
  use Magento\Framework\Setup\SchemaSetupInterface;
  use Magento\Framework\Setup\ModuleContextInterface;
  use Magento\Framework\DB\Ddl\Table;

  class UpgradeSchema implements  UpgradeSchemaInterface
 {
  public function upgrade(SchemaSetupInterface $setup,  ModuleContextInterface $context)
{
   $setup->startSetup();

    if (version_compare($context->getVersion(), '2.0.1', '<')) {
      $setup->getConnection()->addColumn(
        $setup->getTable("product_temp_cart"),
        'id',
        [
          "type" => Table::TYPE_INTEGER,
          "identity" => true,
          "nullable" => false,
          "primary" => true
        ]
      );
      $setup->getConnection()->addColumn(
        $setup->getTable("product_temp_cart"),
        'reference_sku',
        [
          "type" => Table::TYPE_TEXT,
          "nullable" => true
        ]
      );
      $setup->getConnection()->addColumn(
        $setup->getTable("product_temp_cart"),
        'product_sku',
        [
          "type" => Table::TYPE_TEXT,
          "nullable" => true
        ]
      );
      $setup->getConnection()->addColumn(
      $setup->getTable("product_temp_cart"),
        'customer_id',
        [
          "type" => Table::TYPE_INTEGER,
          "nullable" => false
        ]
      );
        $setup->getConnection()->addColumn(
        $setup->getTable("product_temp_cart"),
        'quote_id',
        [
          "type" => Table::TYPE_INTEGER,
          "nullable" => true
        ]
      );
       $setup->getConnection()->addColumn(
        $setup->getTable("product_temp_cart"),
        'quote_id',
        [
          "type" => Table::TYPE_TIMESTAMP,
          "nullable" => true
        ]
      );

    }
    $setup->endSetup();
    }
 }

Is it possible to create a table within an existing module?

If yes can anyone please share the solution.

Thanks in advance

1

2 Answers 2

2

You can create a table by InstallSchema.php

If you forgot to create a table first time, You have to delete module entry from setup_module table. You can find your module name like Namespace_Modulename in this table. just delete your module entry and run below command

php bin/magento setup:upgrade

It will create your table.

9
  • Is any other possibilities, without deleting module from setup? Jan 24, 2019 at 17:00
  • Yes. You can create by UpgradeSchema.php. Jan 24, 2019 at 17:03
  • If you are creating by UpgradeSchema.php, you have to add same version to module.xml. Jan 24, 2019 at 19:31
  • Can you pls update me code Jan 25, 2019 at 2:34
  • Which code you need to update? Jan 25, 2019 at 2:42
2

UpgradeScheme only works when you need to update your existing table. If your existing module does not contain the table yet, what you need to do is find the setup_module table in your Database.

  • Find the Entry of your module by name.
  • Delete the entry of your module.
  • Create Setup/InstallSchema.php, create the complete script.
  • Then run setup:

php bin/magento setup:upgrade

This will then create the non-existent table (from InstallSchema) and run UpgradeSchema right afterwards for version-specific upgrades.

1

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.