I made a simple module wich create a simple table.
This is my module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <module name="Tutorial_TutorialTable" setup_version="1.0.0" active="true">


This is my InstallSchema.php under Tutorial/TutorialTable/Setup/


namespace Tutorial\TutorialTable\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
        public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
            $installer = $setup;

            // Get tutorial_simplenews table
            $tableName = $installer->getTable('tutorial_simplenews');
            // Check if the table already exists
            if ($installer->getConnection()->isTableExists($tableName) != true) {
                // Create tutorial_simplenews table
                $table = $installer->getConnection()
                            'identity' => true,
                            'unsigned' => true,
                            'nullable' => false,
                            'primary' => true

and so on...

The table is correctly created.

Now I want to use UpgradeData.php to insert into table something and I add UpgradeData.php under Tutorial/TutorialTable/Setup


namespace Tutorial\TutorialTable\Setup;

use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class UpgradeData implements UpgradeDataInterface
    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        if (version_compare($context->getVersion(), '1.0.1') == 0) {
            // Get tutorial_simplenews table
            $tableName = $setup->getTable('tutorial_simplenews');
            // Check if the table already exists
            if ($setup->getConnection()->isTableExists($tableName) == true) {

.. add some data in $data array .. and then

   foreach ($data as $item) {
                    $setup->getConnection()->insert($tableName, $item);


Si i edit the setup_version in my module.xml


and then i run

php bin/magento setup:upgrade

And all module is correctly load but i don't have any data in my table.
Where i'm wrong? Thanks in advice.

1 Answer 1


I think this is the problem

if (version_compare($context->getVersion(), '1.0.1') == 0) {

it should be

if (version_compare($context->getVersion(), '1.0.1') < 0) {

because when you run the upgradeData file the version is still 1.0.0 and it becomes 1.0.1 only after the upgrade is done.

  • 8
    You can even use the third parameter of version_compare: (version_compare($context->getVersion(), '1.0.1','<') Feb 11, 2016 at 15:32

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.