How to add new table in existing module using UpgradeSchema in Magento 2

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












class UpgradeSchema implements UpgradeSchemaInterface

public function upgrade(SchemaSetupInterface $setup,ModuleContextInterface $context)
$setup->startSetup();
if (version_compare($context->getVersion(), '2.0.0', '<='))
$installer = $setup;
$installer->startSetup();

$table = $installer->getConnection()
->newTable($installer->getTable('my_booster'))
->addColumn(
'review_id',
Table::TYPE_SMALLINT,
null,
['identity' => true, 'nullable' => false, 'primary' => true],
'Review ID'
)
->addColumn('pros', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I like about this product')
->addColumn('cons', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I dislike about this product')
->addColumn('title', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Title')
->addColumn('review_comments', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Comments')
->addColumn('send_to', Table::TYPE_TEXT, 255, ['nullable' => false], 'Send reply to Customer')
->addColumn('helpful', Table::TYPE_TEXT, 255, ['nullable' => false], 'Was this review helpful?')
->addColumn('product_quality', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Quality ')
->addColumn('Product Value', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Value')
->addColumn('product_price', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Price')
->addColumn('image', Table::TYPE_TEXT, 255, ['nullable' => false], 'Viemo');

$installer->getConnection()->createTable($table);
$installer->endSetup();






I am using this script.







share|improve this question


























    up vote
    2
    down vote

    favorite












    class UpgradeSchema implements UpgradeSchemaInterface

    public function upgrade(SchemaSetupInterface $setup,ModuleContextInterface $context)
    $setup->startSetup();
    if (version_compare($context->getVersion(), '2.0.0', '<='))
    $installer = $setup;
    $installer->startSetup();

    $table = $installer->getConnection()
    ->newTable($installer->getTable('my_booster'))
    ->addColumn(
    'review_id',
    Table::TYPE_SMALLINT,
    null,
    ['identity' => true, 'nullable' => false, 'primary' => true],
    'Review ID'
    )
    ->addColumn('pros', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I like about this product')
    ->addColumn('cons', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I dislike about this product')
    ->addColumn('title', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Title')
    ->addColumn('review_comments', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Comments')
    ->addColumn('send_to', Table::TYPE_TEXT, 255, ['nullable' => false], 'Send reply to Customer')
    ->addColumn('helpful', Table::TYPE_TEXT, 255, ['nullable' => false], 'Was this review helpful?')
    ->addColumn('product_quality', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Quality ')
    ->addColumn('Product Value', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Value')
    ->addColumn('product_price', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Price')
    ->addColumn('image', Table::TYPE_TEXT, 255, ['nullable' => false], 'Viemo');

    $installer->getConnection()->createTable($table);
    $installer->endSetup();






    I am using this script.







    share|improve this question






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      class UpgradeSchema implements UpgradeSchemaInterface

      public function upgrade(SchemaSetupInterface $setup,ModuleContextInterface $context)
      $setup->startSetup();
      if (version_compare($context->getVersion(), '2.0.0', '<='))
      $installer = $setup;
      $installer->startSetup();

      $table = $installer->getConnection()
      ->newTable($installer->getTable('my_booster'))
      ->addColumn(
      'review_id',
      Table::TYPE_SMALLINT,
      null,
      ['identity' => true, 'nullable' => false, 'primary' => true],
      'Review ID'
      )
      ->addColumn('pros', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I like about this product')
      ->addColumn('cons', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I dislike about this product')
      ->addColumn('title', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Title')
      ->addColumn('review_comments', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Comments')
      ->addColumn('send_to', Table::TYPE_TEXT, 255, ['nullable' => false], 'Send reply to Customer')
      ->addColumn('helpful', Table::TYPE_TEXT, 255, ['nullable' => false], 'Was this review helpful?')
      ->addColumn('product_quality', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Quality ')
      ->addColumn('Product Value', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Value')
      ->addColumn('product_price', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Price')
      ->addColumn('image', Table::TYPE_TEXT, 255, ['nullable' => false], 'Viemo');

      $installer->getConnection()->createTable($table);
      $installer->endSetup();






      I am using this script.







      share|improve this question












      class UpgradeSchema implements UpgradeSchemaInterface

      public function upgrade(SchemaSetupInterface $setup,ModuleContextInterface $context)
      $setup->startSetup();
      if (version_compare($context->getVersion(), '2.0.0', '<='))
      $installer = $setup;
      $installer->startSetup();

      $table = $installer->getConnection()
      ->newTable($installer->getTable('my_booster'))
      ->addColumn(
      'review_id',
      Table::TYPE_SMALLINT,
      null,
      ['identity' => true, 'nullable' => false, 'primary' => true],
      'Review ID'
      )
      ->addColumn('pros', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I like about this product')
      ->addColumn('cons', Table::TYPE_TEXT, 255, ['nullable' => false], 'What I dislike about this product')
      ->addColumn('title', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Title')
      ->addColumn('review_comments', Table::TYPE_TEXT, 255, ['nullable' => false], 'Custom Comments')
      ->addColumn('send_to', Table::TYPE_TEXT, 255, ['nullable' => false], 'Send reply to Customer')
      ->addColumn('helpful', Table::TYPE_TEXT, 255, ['nullable' => false], 'Was this review helpful?')
      ->addColumn('product_quality', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Quality ')
      ->addColumn('Product Value', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Value')
      ->addColumn('product_price', Table::TYPE_TEXT, 255, ['nullable' => false], 'Product Price')
      ->addColumn('image', Table::TYPE_TEXT, 255, ['nullable' => false], 'Viemo');

      $installer->getConnection()->createTable($table);
      $installer->endSetup();






      I am using this script.









      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 13 at 22:34









      Ali Ejaz

      1209




      1209




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          Try this



           class UpgradeSchema implements UpgradeSchemaInterface


          {



          public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

          $installer = $setup;
          $setup->startSetup();

          $version = $context->getVersion();
          $connection = $setup->getConnection();

          if (version_compare($version, '2.0.0') < 0)
          $connection->addColumn(
          $setup->getTable('my_booster'),
          'author_id',
          [
          'type' => MagentoFrameworkDBDdlTable::TYPE_INTEGER,
          'nullable' => true,
          'comment' => 'Author ID',

          ]
          );








          share|improve this answer






















          • kindly mention the issue as well.
            – Ali Ejaz
            Aug 13 at 22:46










          • here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing
            – sheraz khan
            Aug 13 at 22:48











          • try this if (version_compare($version, '2.0.0') < 0)
            – sheraz khan
            Aug 13 at 22:49










          • your answer is not working for me.
            – Ali Ejaz
            Aug 13 at 22:53

















          up vote
          1
          down vote



          accepted










          i have got the correct working answer.



          <?php

          namespace VendorModuleSetup;

          use MagentoFrameworkSetupUpgradeSchemaInterface;
          use MagentoFrameworkSetupSchemaSetupInterface;
          use MagentoFrameworkSetupModuleContextInterface;
          use MagentoFrameworkDBDdlTable;


          class UpgradeSchema implements UpgradeSchemaInterface

          public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

          $setup->startSetup();

          if (version_compare($context->getVersion(), '2.0.2', '<'))
          $setup->getConnection()->addColumn(
          $setup->getTable("enter table name here"),
          'enter new column name here',
          [
          "type" => Table::TYPE_TEXT,
          "nullable" => true,
          "comment" => "xample"
          ]
          );
          $setup->getConnection()->addColumn(
          $setup->getTable("enter table name here"),
          'enter new second column name here',
          [
          "type" => Table::TYPE_TEXT,
          "nullable" => true,
          "comment" => "Xample 2"
          ]
          );
          $setup->getConnection()->addColumn(
          $setup->getTable("enter table name here"),
          'enter new third column name here',
          [
          "type" => Table::TYPE_TEXT,
          "nullable" => true,
          "comment" => "Xample 3"
          ]
          );


          $setup->endSetup();




          also you need to change etc/module.xml file. where setup_version="2.0.1" schema_version="2.0.1" .






          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "479"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f238251%2fhow-to-add-new-table-in-existing-module-using-upgradeschema-in-magento-2%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            Try this



             class UpgradeSchema implements UpgradeSchemaInterface


            {



            public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

            $installer = $setup;
            $setup->startSetup();

            $version = $context->getVersion();
            $connection = $setup->getConnection();

            if (version_compare($version, '2.0.0') < 0)
            $connection->addColumn(
            $setup->getTable('my_booster'),
            'author_id',
            [
            'type' => MagentoFrameworkDBDdlTable::TYPE_INTEGER,
            'nullable' => true,
            'comment' => 'Author ID',

            ]
            );








            share|improve this answer






















            • kindly mention the issue as well.
              – Ali Ejaz
              Aug 13 at 22:46










            • here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing
              – sheraz khan
              Aug 13 at 22:48











            • try this if (version_compare($version, '2.0.0') < 0)
              – sheraz khan
              Aug 13 at 22:49










            • your answer is not working for me.
              – Ali Ejaz
              Aug 13 at 22:53














            up vote
            2
            down vote













            Try this



             class UpgradeSchema implements UpgradeSchemaInterface


            {



            public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

            $installer = $setup;
            $setup->startSetup();

            $version = $context->getVersion();
            $connection = $setup->getConnection();

            if (version_compare($version, '2.0.0') < 0)
            $connection->addColumn(
            $setup->getTable('my_booster'),
            'author_id',
            [
            'type' => MagentoFrameworkDBDdlTable::TYPE_INTEGER,
            'nullable' => true,
            'comment' => 'Author ID',

            ]
            );








            share|improve this answer






















            • kindly mention the issue as well.
              – Ali Ejaz
              Aug 13 at 22:46










            • here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing
              – sheraz khan
              Aug 13 at 22:48











            • try this if (version_compare($version, '2.0.0') < 0)
              – sheraz khan
              Aug 13 at 22:49










            • your answer is not working for me.
              – Ali Ejaz
              Aug 13 at 22:53












            up vote
            2
            down vote










            up vote
            2
            down vote









            Try this



             class UpgradeSchema implements UpgradeSchemaInterface


            {



            public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

            $installer = $setup;
            $setup->startSetup();

            $version = $context->getVersion();
            $connection = $setup->getConnection();

            if (version_compare($version, '2.0.0') < 0)
            $connection->addColumn(
            $setup->getTable('my_booster'),
            'author_id',
            [
            'type' => MagentoFrameworkDBDdlTable::TYPE_INTEGER,
            'nullable' => true,
            'comment' => 'Author ID',

            ]
            );








            share|improve this answer














            Try this



             class UpgradeSchema implements UpgradeSchemaInterface


            {



            public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

            $installer = $setup;
            $setup->startSetup();

            $version = $context->getVersion();
            $connection = $setup->getConnection();

            if (version_compare($version, '2.0.0') < 0)
            $connection->addColumn(
            $setup->getTable('my_booster'),
            'author_id',
            [
            'type' => MagentoFrameworkDBDdlTable::TYPE_INTEGER,
            'nullable' => true,
            'comment' => 'Author ID',

            ]
            );









            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Aug 13 at 22:47

























            answered Aug 13 at 22:43









            sheraz khan

            918




            918











            • kindly mention the issue as well.
              – Ali Ejaz
              Aug 13 at 22:46










            • here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing
              – sheraz khan
              Aug 13 at 22:48











            • try this if (version_compare($version, '2.0.0') < 0)
              – sheraz khan
              Aug 13 at 22:49










            • your answer is not working for me.
              – Ali Ejaz
              Aug 13 at 22:53
















            • kindly mention the issue as well.
              – Ali Ejaz
              Aug 13 at 22:46










            • here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing
              – sheraz khan
              Aug 13 at 22:48











            • try this if (version_compare($version, '2.0.0') < 0)
              – sheraz khan
              Aug 13 at 22:49










            • your answer is not working for me.
              – Ali Ejaz
              Aug 13 at 22:53















            kindly mention the issue as well.
            – Ali Ejaz
            Aug 13 at 22:46




            kindly mention the issue as well.
            – Ali Ejaz
            Aug 13 at 22:46












            here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing
            – sheraz khan
            Aug 13 at 22:48





            here is the issue if (version_compare($context->getVersion(), '2.0.0', '<=')) , withi which you are comparing
            – sheraz khan
            Aug 13 at 22:48













            try this if (version_compare($version, '2.0.0') < 0)
            – sheraz khan
            Aug 13 at 22:49




            try this if (version_compare($version, '2.0.0') < 0)
            – sheraz khan
            Aug 13 at 22:49












            your answer is not working for me.
            – Ali Ejaz
            Aug 13 at 22:53




            your answer is not working for me.
            – Ali Ejaz
            Aug 13 at 22:53












            up vote
            1
            down vote



            accepted










            i have got the correct working answer.



            <?php

            namespace VendorModuleSetup;

            use MagentoFrameworkSetupUpgradeSchemaInterface;
            use MagentoFrameworkSetupSchemaSetupInterface;
            use MagentoFrameworkSetupModuleContextInterface;
            use MagentoFrameworkDBDdlTable;


            class UpgradeSchema implements UpgradeSchemaInterface

            public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

            $setup->startSetup();

            if (version_compare($context->getVersion(), '2.0.2', '<'))
            $setup->getConnection()->addColumn(
            $setup->getTable("enter table name here"),
            'enter new column name here',
            [
            "type" => Table::TYPE_TEXT,
            "nullable" => true,
            "comment" => "xample"
            ]
            );
            $setup->getConnection()->addColumn(
            $setup->getTable("enter table name here"),
            'enter new second column name here',
            [
            "type" => Table::TYPE_TEXT,
            "nullable" => true,
            "comment" => "Xample 2"
            ]
            );
            $setup->getConnection()->addColumn(
            $setup->getTable("enter table name here"),
            'enter new third column name here',
            [
            "type" => Table::TYPE_TEXT,
            "nullable" => true,
            "comment" => "Xample 3"
            ]
            );


            $setup->endSetup();




            also you need to change etc/module.xml file. where setup_version="2.0.1" schema_version="2.0.1" .






            share|improve this answer
























              up vote
              1
              down vote



              accepted










              i have got the correct working answer.



              <?php

              namespace VendorModuleSetup;

              use MagentoFrameworkSetupUpgradeSchemaInterface;
              use MagentoFrameworkSetupSchemaSetupInterface;
              use MagentoFrameworkSetupModuleContextInterface;
              use MagentoFrameworkDBDdlTable;


              class UpgradeSchema implements UpgradeSchemaInterface

              public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

              $setup->startSetup();

              if (version_compare($context->getVersion(), '2.0.2', '<'))
              $setup->getConnection()->addColumn(
              $setup->getTable("enter table name here"),
              'enter new column name here',
              [
              "type" => Table::TYPE_TEXT,
              "nullable" => true,
              "comment" => "xample"
              ]
              );
              $setup->getConnection()->addColumn(
              $setup->getTable("enter table name here"),
              'enter new second column name here',
              [
              "type" => Table::TYPE_TEXT,
              "nullable" => true,
              "comment" => "Xample 2"
              ]
              );
              $setup->getConnection()->addColumn(
              $setup->getTable("enter table name here"),
              'enter new third column name here',
              [
              "type" => Table::TYPE_TEXT,
              "nullable" => true,
              "comment" => "Xample 3"
              ]
              );


              $setup->endSetup();




              also you need to change etc/module.xml file. where setup_version="2.0.1" schema_version="2.0.1" .






              share|improve this answer






















                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                i have got the correct working answer.



                <?php

                namespace VendorModuleSetup;

                use MagentoFrameworkSetupUpgradeSchemaInterface;
                use MagentoFrameworkSetupSchemaSetupInterface;
                use MagentoFrameworkSetupModuleContextInterface;
                use MagentoFrameworkDBDdlTable;


                class UpgradeSchema implements UpgradeSchemaInterface

                public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

                $setup->startSetup();

                if (version_compare($context->getVersion(), '2.0.2', '<'))
                $setup->getConnection()->addColumn(
                $setup->getTable("enter table name here"),
                'enter new column name here',
                [
                "type" => Table::TYPE_TEXT,
                "nullable" => true,
                "comment" => "xample"
                ]
                );
                $setup->getConnection()->addColumn(
                $setup->getTable("enter table name here"),
                'enter new second column name here',
                [
                "type" => Table::TYPE_TEXT,
                "nullable" => true,
                "comment" => "Xample 2"
                ]
                );
                $setup->getConnection()->addColumn(
                $setup->getTable("enter table name here"),
                'enter new third column name here',
                [
                "type" => Table::TYPE_TEXT,
                "nullable" => true,
                "comment" => "Xample 3"
                ]
                );


                $setup->endSetup();




                also you need to change etc/module.xml file. where setup_version="2.0.1" schema_version="2.0.1" .






                share|improve this answer












                i have got the correct working answer.



                <?php

                namespace VendorModuleSetup;

                use MagentoFrameworkSetupUpgradeSchemaInterface;
                use MagentoFrameworkSetupSchemaSetupInterface;
                use MagentoFrameworkSetupModuleContextInterface;
                use MagentoFrameworkDBDdlTable;


                class UpgradeSchema implements UpgradeSchemaInterface

                public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)

                $setup->startSetup();

                if (version_compare($context->getVersion(), '2.0.2', '<'))
                $setup->getConnection()->addColumn(
                $setup->getTable("enter table name here"),
                'enter new column name here',
                [
                "type" => Table::TYPE_TEXT,
                "nullable" => true,
                "comment" => "xample"
                ]
                );
                $setup->getConnection()->addColumn(
                $setup->getTable("enter table name here"),
                'enter new second column name here',
                [
                "type" => Table::TYPE_TEXT,
                "nullable" => true,
                "comment" => "Xample 2"
                ]
                );
                $setup->getConnection()->addColumn(
                $setup->getTable("enter table name here"),
                'enter new third column name here',
                [
                "type" => Table::TYPE_TEXT,
                "nullable" => true,
                "comment" => "Xample 3"
                ]
                );


                $setup->endSetup();




                also you need to change etc/module.xml file. where setup_version="2.0.1" schema_version="2.0.1" .







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 16 at 23:28









                Ali Ejaz

                1209




                1209



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f238251%2fhow-to-add-new-table-in-existing-module-using-upgradeschema-in-magento-2%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    Long meetings (6-7 hours a day): Being “babysat” by supervisor

                    Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                    Confectionery