php - SQLSTATE [HY000]:一般错误:3780 外键约束中引用列“store_id”和引用列“store_id”不兼容
问题描述
运行 php bin/magento setup:upgrade command (magento 2.4) SQLSTATE[HY000]: General error: 3780 Reference column 'store_id' and referenced column 'store_id' in Foreign key constraint不兼容。
<?php
namespace Dealer\Sellout\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
{
/**
* {@inheritdoc}
*/
public function install(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
/**
* Create table 'sell-out-order'
*/
$table = $setup->getConnection()->newTable(
$setup->getTable('sell-out-order')
)->addColumn(
'id',
Table::TYPE_INTEGER,
null,
['identity' => true, 'nullable' => false, 'primary' => true],
'SellOut ID'
)->addColumn(
'company_user_id',
Table::TYPE_INTEGER,
255,
['nullable' => false],
'Company User Id'
)->addColumn(
'customer_id',
Table::TYPE_INTEGER,
255,
['nullable' => false],
'Customer Id'
)->addColumn(
'store_id',
Table::TYPE_INTEGER,
255,
['nullable' => false],
'Store Id'
)->addIndex(
$setup->getIdxName('sell-out-order', ['customer_id']),
['customer_id']
)->setComment(
'Sell Outs'
)->addForeignKey(
$setup->getFkName('sell-out-order', 'store_id', 'store', 'store_id'),
'store_id',
$setup->getTable('store'),
'store_id',
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
)->addForeignKey(
$setup->getFkName('sell-out-order', 'customer_id', 'customer_entity', 'customer_id'),
'customer_id',
$setup->getTable('customer_entity'),
'customer_id',
\Magento\Framework\DB\Ddl\Table::ACTION_CASCADE
);
$setup->getConnection()->createTable($table);
/**
* Create table 'sell-out-order-item'
*/
$table = $setup->getConnection()->newTable(
$setup->getTable('sell-out-order-item')
)->addColumn(
'id',
Table::TYPE_INTEGER,
null,
['identity' => true, 'nullable' => false, 'primary' => true],
'SellOut Order Item ID'
)->addColumn(
'sell-out-order_id',
Table::TYPE_INTEGER,
255,
['nullable' => false],
'SellOut Order Id'
)->addColumn(
'product_id',
Table::TYPE_INTEGER,
255,
['nullable' => false],
'Product Id'
)->addColumn(
'name',
Table::TYPE_TEXT,
255,
['nullable' => false],
'Product SKU'
)->addColumn(
'product_type',
Table::TYPE_TEXT,
255,
['nullable' => false],
'Product SKU'
)->addColumn(
'sku',
Table::TYPE_INTEGER,
255,
['nullable' => false],
'Product SKU'
)->addColumn(
'qty',
Table::TYPE_INTEGER,
255,
['nullable' => false],
'Qty'
)->addColumn(
'store_id',
Table::TYPE_INTEGER,
255,
['nullable' => false],
'Store Id'
)->addIndex(
$setup->getIdxName('sell-out-order-item', ['sku']),
['sku']
)->setComment(
'Sell Out Items'
)->addForeignKey(
$setup->getFkName('sell-out-order-item', 'store_id', 'store', 'store_id'),
'store_id',
$setup->getTable('store'),
'store_id',
\Magento\Framework\DB\Ddl\Table::ACTION_NO_ACTION
)->addForeignKey(
$setup->getFkName('sell-out-order-item', 'sell-out-order_id', 'sell-out-order', 'sell-out-order_id'),
'sell-out-order_id',
$setup->getTable('sell-out-order'),
'sell-out-order_id',
\Magento\Framework\DB\Ddl\Table::ACTION_NO_ACTION
);
$setup->getConnection()->createTable($table);
$setup->endSetup();
}
}
解决方案
我猜您使用的是 Laravel 5.8 或更高版本。这个问题起初在 Laravel 更新文档中从未提及。
这是一个列定义不匹配,会在“不兼容的类型”上引发错误。您的“store_id”列的类型是“smallint”,与所引用列的“store_id”不兼容(?)。如果您确保两者的类型相同,则可以避免此错误。你能发布参考表结构吗?
仔细检查每次迁移定义的类型。
更多在这里,
https://logic.edchen.org/how-to-resolve-cannot-add-foreign-key-constraint-in-mysql/
推荐阅读
- jquery - 单击链接无法提交jquery表单
- postgresql - sqlite vs postgresql - 数据库大小比较
- unicode - 如何从保存为 ANSI 文本文件的记事本文件中检索 Unicode 文本
- r - R SUR 回归使用 systemfit 导致错误:LU 计算奇异:极端条目的比率
- pandas - Pandas .replace() int by boolean
- c# - 这段代码是什么意思:public bool HasBars => BarList != null && BarList.Count > 0;?
- javascript - 为什么带有显示/隐藏的 for 循环在 Vue JS 中不起作用?
- crf - 深度语义分割和条件随机场
- kubernetes - Minikube 引导失败
- java - 游戏循环滞后