You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter 4迁移失败但等效MySQL查询正常的问题求助

CodeIgniter 4迁移创建失败:productCode外键列不兼容问题

我是CodeIgniter新手,在CodeIgniter 4中创建购物车表的迁移时一直失败,报错提示外键约束中productCode列不兼容,但直接执行等效的MySQL语句却能成功创建表。

迁移文件代码

文件名:2024-03-21-091038_CreateCartTable.php

class CreateCartTable extends Migration
{
    public function up()
    {
        $this->forge->addField([
            'id' => [
                'type' => 'INT',
                'unsigned' => true,
                'auto_increment' => true,
                'null' => false,
            ],
            'productCode' => [
                'type' => 'VARCHAR',
                'constraint' => '15',
                'null' => false,
            ],
            'quantity' => [
                'type' => 'INT',
                'default' => 1,
                'null' => false,
            ],
            'customerNumber' => [
                'type' => 'INT',
                'null' => false,
            ],
            'createdAt' => [
                'type' => 'TIMESTAMP',
                'default' => 'CURRENT_TIMESTAMP',
            ],
            'updatedAt' => [
                'type' => 'TIMESTAMP',
                'default' => 'CURRENT_TIMESTAMP',
                'onUpdate' => 'CURRENT_TIMESTAMP',
            ],
        ]);

        $this->forge->addKey('id', true);

        $this->forge->addForeignKey('customerNumber', 'customers', 'customerNumber');
        $this->forge->addForeignKey('productCode', 'products', 'productCode');

        $this->forge->createTable('cart', true);
    }

    public function down()
    {
        $this->forge->dropTable('cart', true);
    }
}

执行迁移命令的报错

执行命令:php spark migrate:refresh

$ php spark migrate:refresh

CodeIgniter v4.4.6 Command Line Tool - Server Time: 2024-03-21 12:52:32 UTC+00:00

Rolling back migrations to batch:  0
        Rolling back: (App) 2024-03-14-135404_App\Database\Migrations\AddOfficeLatLon
        Rolling back: (App) 2024-03-14-111807_App\Database\Migrations\AddAboutToOffices
Done rolling back migrations.
Running all new migrations...
[CodeIgniter\Database\Exceptions\DatabaseException]
Referencing column 'productCode' and referenced column 'productCode' in foreign key constraint 'cart_productCode_foreign' are incompatible.
at SYSTEMPATH/Database/BaseConnection.php:647

  Caused by:
  [CodeIgniter\Database\Exceptions\DatabaseException]
  Referencing column 'productCode' and referenced column 'productCode' in foreign key constraint 'cart_productCode_foreign' are incompatible.
  at SYSTEMPATH/Database/MySQLi/Connection.php:311

  Caused by:
  [mysqli_sql_exception]
  Referencing column 'productCode' and referenced column 'productCode' in foreign key constraint 'cart_productCode_foreign' are incompatible.
  at SYSTEMPATH/Database/MySQLi/Connection.php:306

等效的MySQL创建语句

直接执行该SQL可成功创建表:

CREATE TABLE classicmodels.cart (
    id INT UNSIGNED AUTO_INCREMENT NOT NULL,
    productCode VARCHAR(15) NOT NULL,
    quantity INT DEFAULT 1 NOT NULL,
    customerNumber INT NOT NULL ,
    createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    PRIMARY KEY (id),
    CONSTRAINT FOREIGN KEY (customerNumber) REFERENCES customers(customerNumber),
    CONSTRAINT FOREIGN KEY (productCode) REFERENCES products(productCode)
);

执行结果:Query OK, 0 rows affected (0.04 sec)

补充信息

  • 注释createdAt、updatedAt字段及productCode外键约束后,迁移可正常执行
  • 此前还出现过Invalid default value for 'createdAt'错误
  • CodeIgniter控制台显示的时区与本地时区不符

内容的提问来源于stack exchange,提问作者Findus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 16:05:03