错误:运行setup:upgrade时,事务中不允许使用DDL语句。运行数据补丁

时间:2019-06-06 22:58:55

标签: magento2 magento-2.3

Magento 2.3.1

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

供应商/模块名称/设置/补丁/数据/AddMyColumnPatch.php

下面为AddMyColumnPatch.php给出的

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

  

交易中不允许使用DDL语句

我已使用以下文件作为参考,使用数据补丁将列添加到表中。

  

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 [];
    }
}

1 个答案:

答案 0 :(得分:0)

再次浏览了声明式架构文档并引用了Quote模块和Paypal模块的magento核心代码后,我发现如果要将字段添加到Magento 2.3的现有表中,则应使用配置声明式架构。阅读更多-

[https://devdocs.magento.com/guides/v2.3/extension-dev-guide/declarative-schema/db-schema.html][1]

因此在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_address和order_sales_address表中。

bin/magento setup:upgrade

但是,进一步的调查显示,不需要为在平面表中添加列quote_address和sales_order_address进行数据修补。仅在db_schema.xml中声明列即可。