首页 > 解决方案 > SQLSTATE [HY000]:一般错误:3780 外键约束中引用列“store_id”和引用列“store_id”不兼容

问题描述

phpMyAdmin 中的表结构

运行 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();
    }
}

标签: phpmagentomagento2

解决方案


我猜您使用的是 Laravel 5.8 或更高版本。这个问题起初在 Laravel 更新文档中从未提及。

这是一个列定义不匹配,会在“不兼容的类型”上引发错误。您的“store_id”列的类型是“smallint”,与所引用列的“store_id”不兼容(?)。如果您确保两者的类型相同,则可以避免此错误。你能发布参考表结构吗?

仔细检查每次迁移定义的类型。

更多在这里,

https://logic.edchen.org/how-to-resolve-cannot-add-foreign-key-constraint-in-mysql/


推荐阅读