CodeIgniter 4执行migrate报错:where子句无namespace列
解决CodeIgniter迁移报错“Unknown column 'namespace' in 'where clause'”
问题根源
这个报错是因为CodeIgniter在查询迁移记录表(默认表名migrations)时需要namespace字段,但你的表结构中缺少该字段——通常是项目从CodeIgniter 4.3以下版本升级导致的,旧版本迁移表无此字段。
同时你的迁移代码存在两处语法错误(虽不直接触发当前报错,但会影响表创建):
id字段中重复且拼写错误的usigned参数;created_at字段用字符串形式定义,不符合CodeIgniter字段规范。
解决步骤
1. 修复迁移表结构
执行SQL语句为migrations表添加namespace字段:
ALTER TABLE `migrations` ADD COLUMN `namespace` varchar(255) NOT NULL DEFAULT 'App\Database\Migrations';
2. 修正迁移代码
替换为以下修正后的代码:
<?php namespace App\Database\Migrations; use CodeIgniter\Database\Migration; class Car extends Migration { public function up() { $this->forge->addField([ 'id' => [ 'type' => 'INT', 'constraint' => 5, 'unsigned' => true, 'auto_increment' => true ], 'parent_id' => ['type' => 'INT'], 'name' => ['type' => 'VARCHAR', 'constraint' => 200], 'email' => ['type' => 'VARCHAR', 'constraint' => 200], 'password' => ['type' => 'VARCHAR', 'constraint' => 200], 'created_at' => [ 'type' => 'DATETIME', 'default' => 'CURRENT_TIMESTAMP' ], ]); $this->forge->addKey('id', true); $this->forge->createTable('car'); } public function down() { $this->forge->dropTable('car'); } }
3. 重新执行迁移
运行命令完成迁移:
php spark migrate
内容的提问来源于stack exchange,提问作者Mr. Paul
相关产品推荐
相关产品推荐

