gpt4 book ai didi

magento2 - 错误 : DDL statements are not allowed in transactions while running setup:upgrade. 运行数据补丁

转载 作者:行者123 更新时间:2023-12-05 05:10:33 24 4
gpt4 key购买 nike

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

我使用以下文件作为引用,使用数据补丁将一列添加到我的表中:

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 中的现有表中,您应该为此使用配置声明性架构.阅读更多:

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 表中。

bin/magento setup:upgrade

但是,进一步调查发现,在平面表 quote_addresssales_order_address 中添加列不需要制作数据补丁。只有在 db_schema.xml 中声明列才能完成这项工作。

关于magento2 - 错误 : DDL statements are not allowed in transactions while running setup:upgrade. 运行数据补丁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56486048/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com