Error: DDL statements are not allowed in transactions while running setup:upgrade. Running Data Patch

Error: DDL statements are not allowed in transactions while running setup:upgrade. Running Data Patch

Magento 2.3.1

在以下位置制作数据补丁后:

vendor/ModuleName/Setup/Patch/Data/AddMyColumnPatch.php

(下面给出 AddMyColumnPatch.php 的代码)

当我 运行 bin/magento setup:upgrade 安装此补丁时,我在 cli 出现以下错误:

DDL statements are not allowed in transactions

我使用以下文件作为参考,使用数据补丁向我的 table 添加一列:

vendor/magento/module-quote/Setup/Patch/Data/InstallEntityTypes.php

按照第 47 行到第 65 行。

我的AddMyColumnPatch.php代码是:

<?php
namespace Vendor\ModuleName\Setup\Patch\Data;


use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\PatchRevertableInterface;

use Magento\Quote\Setup\QuoteSetupFactory;
use Magento\Sales\Setup\SalesSetupFactory;

use Psr\Log\LoggerInterface;

/**
 */
class AddressSuburbPatch implements DataPatchInterface, PatchRevertableInterface
{
    /**
     * Attribute Code of the Custom Attribute
     */
    const CUSTOM_ATTRIBUTE_CODE = 'my_column';

    /**
     * @var \Magento\Framework\Setup\ModuleDataSetupInterface
     */
    private $moduleDataSetup;

    /**
     * @var \Magento\Quote\Setup\QuoteSetupFactory
     */
    private $quoteSetupFactory;


    /**
     * @var Magento\Sales\Setup\SalesSetupFactory
     */
    private $salesSetupFactory;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    private $logger;

    /**
     * @param \Magento\Framework\Setup\ModuleDataSetupInterface $moduleDataSetup
     */
    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        QuoteSetupFactory $quoteSetupFactory,
        SalesSetupFactory $salesSetupFactory,    
        LoggerInterface $logger
    )
    {

        $this->moduleDataSetup = $moduleDataSetup;          
        $this->quoteSetupFactory = $quoteSetupFactory;
        $this->salesSetupFactory = $salesSetupFactory;            
        $this->logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $this->moduleDataSetup->getConnection()->startSetup();


        $this->logger->debug('DDL Statements error');

        $quoteSetup = $this->quoteSetupFactory->create(['setup' => $this->moduleDataSetup]);

        $quoteSetup->addAttribute('quote_address', self::CUSTOM_ATTRIBUTE_CODE, ['type' => 'text']);

        $salesSetup = $this->salesSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $salesSetup->addAttribute('order_address', self::CUSTOM_ATTRIBUTE_CODE, ['type' => 'text']);

        $this->logger->debug('Script working');

        $this->moduleDataSetup->getConnection()->endSetup();

    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {

        return [

        ];
    }

    public function revert()
    {
        $this->moduleDataSetup->getConnection()->startSetup();

        $this->moduleDataSetup->getConnection()->endSetup();
    }

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {

        return [];
    }
}

在再次查看声明性架构文档并引用报价模块和 PayPal 模块的核心 Magento 代码后,我发现如果您想将字段添加到 Magento >=2.3 中的现有 table 中,您应该为此使用配置声明模式。阅读更多:

https://devdocs.magento.com/guides/v2.4/extension-dev-guide/declarative-schema/db-schema.html

所以在Vendor/ModuleName/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="quote_address" resource="checkout" comment="Sales Flat Quote Address">
        <column xsi:type="varchar" name="suburb" nullable="true" length="255" comment="Suburb for Quote Address" />
    </table>
    <table name="sales_order_address" resource="sales" comment="Sales Flat Order Address">
        <column xsi:type="varchar" name="mycolumn" nullable="true" length="255" comment="mycolumn for Sales Order Address" />
    </table>
</schema>

然后为您的 db_schema 生成白名单,如下所示:

bin/magento setup:db-declaration:generate-whitelist --module-name=Vendor_ModuleName
再次

运行,您的专栏将添加到 quote_addressorder_sales_address tables.

bin/magento setup:upgrade

然而,进一步的调查显示,在 tables quote_addresssales_order_address 中添加列不需要制作数据补丁。只有在 db_schema.xml 中声明列才能完成这项工作。