0

I need to change the type of the column "value" in the table catalog_product_entity_varchar from varchar(255) to varchar(512), but I find nothing in the documentation about how to do it, so I copy pasted something together from the code I found.

before

app\code\Company\Base\Setup\UpgradeSchema.php:

<?php

namespace Company\Base\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();

        // Wenn Version kleiner als 1.0.1
        if (version_compare($context->getVersion(), '1.0.1', '<')) {

            // Get module table
            $table = $setup->getTable('catalog_product_entity_varchar');
            $connection = $setup->getConnection();

            // Check if the table already exists
            if ($connection->isTableExists($table) == true) {
                
                $connection->modifyColumn(
                    "catalog_product_entity_varchar",
                    'value',
                    [
                        'type' => Table::TYPE_TEXT,
                        'length' => '512',
                    ]
                );
            }
        }

        $setup->endSetup();
    }
}

I also increased the version in my module.xml from 1.0.0 to 1.0.1:

app\code\Company\Base\etc\module.xml:

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Company_Base" setup_version="1.0.1"/>
</config>

Then I executed bin/magento setup:upgrade but it changed it to text instead of varchar(512) and it removed the comment and the Standard NULL:

Before:

before

After:

after

1

Magento replaced the database schema system and replaced it by a better one. It works now with etc/db_schema.xml instead of Setup/UpgradeSchema.php

app\code\Company\Ui\etc\db_schema.xml

<?xml version="1.0"?>
<schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Setup/Declaration/Schema/etc/schema.xsd">
    <table name="catalog_product_entity_varchar" resource="default">
        <column xsi:type="varchar" length="512" name="value" nullable="true" comment="Value"/>
    </table>
</schema>

Then I ran bin/magento setup:upgrade

Note: You can convert your old .php scripts by running bin/magento setup:install --convert-old-scripts=1, this won't work if you added custom SQL though. Read the documentation for more informations.

1
  • 1
    Thank you this helped fix my issue.
    – thindery
    May 24 at 14:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.