0

I want to create a table using UpgradeSchema directly. but on doing so table is not being created. I am attaching the codes below.

code for Upgradeschema.php

<?php
namespace Insync\AddtoCart\Setup;

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

class UpgradeSchema implements UpgradeSchemaInterface
{
  public function upgrade(SchemaSetupInterface $setup, 
  ModuleContextInterface $context)
{
    $setup->startSetup();
    $this->newTable($setup, $context);
    $setup->endSetup();
}
public function newTable($setup, $context)
{
    if (version_compare($context->getVersion(), '1.2.3') < 0)
    {
        $tableName = $setup->getTable('insync_addtocart_upgrade');
        if ($setup->getConnection()->isTableExists($tableName) == true)
        {
            $connection = $setup->getConnection();
            $connection->dropTable($connection->getTableName($tableName));
        }
        if ($setup->getConnection()->isTableExists($tableName) != true)
        {
            $table = $setup->getConnection()
                ->newTable($tableName)
                ->addColumn('id', Table::TYPE_INTEGER, null, [
                    'identity' => true,
                    'unsigned' => true,
                    'nullable' => false,
                    'primary' => true,
                    'auto_increment' => true
                     ], 'id')
                ->addColumn('name',Table::TYPE_TEXT,255,
                    ['nullable => false'],
                    'Name')
                ->addColumn('type', Table::TYPE_TEXT, 255, [
                    'nullable' => false
                     ], 'Type')
                ->addColumn('created_at', 
                    \Magento\Framework\DB\Ddl\Table::TYPE_TIMESTAMP,
                    null,
                    ['nullable' => false, 'default' => \Magento\Framework\DB\Ddl\Table::TIMESTAMP_INIT],
                    'Created At');


            $setup->getConnection()->createTable($table);
        }
    }
}

}

  • are you getting any error in a terminal? share a screenshot if possible – Mohit Rane Aug 21 at 5:45
  • not getting any error. even setup upgrade is running fine. – Naiwrita09 Aug 21 at 5:46
  • 1
    have you tried adding code of newTable() code to upgrade()? – Mohit Rane Aug 21 at 5:52
  • also this line of your code if (version_compare($context->getVersion(), '1.2.3') < 0) is wrong should be if (version_compare($context->getVersion(), '1.2.3', '<')) – Mohit Rane Aug 21 at 5:58
  • 1
    and also chnage module version in module.xml file – Rk Rathod Aug 21 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.