Forge Migrate CodeIgniter 4外键错误

fivyi3re  于 8个月前  发布在  其他
关注(0)|答案(1)|浏览(58)

当使用或迁移Codeigniter生成下面代码中的关系时,出现或错误
迁移产品

public function up()
{

    $this->forge->addField([
        'id'            => [
            'type'           => 'INT',
            'unsigned'       => TRUE,
            'auto_increment' => TRUE
        ],
        'categories_id' => [
            'type'          => 'INT'
        ],
        'product'       => [
            'type'          => 'VARCHAR',
            'constraint'    => '255'
        ]
    ]);
    $this->forge->addKey('id', TRUE);
    $this->forge->createTable('products');
    $this->forge->addForeignKey('categories_id', 'categories', 'id');

}

迁移类别

$this->forge->addField([
        'id'            => [
            'type'           => 'INT',
            'unsigned'       => TRUE,
            'auto_increment' => TRUE
        ],
        'category'      => [
            'type'          => 'VARCHAR',
            'constraint'    => '255'
        ],
        'ordination'    => [
            'type'          => 'INTEGER'
        ],
        'isactive'      => [
            'type'          => 'INTEGER',
            'default'       => 1
        ]
    ]);

    $this->forge->addKey('id', TRUE);
    $this->forge->createTable('categories');

误差
类型:CodeIgniter\Database\SQL\DatabaseException消息:未找到字段categories_id

9njqaruj

9njqaruj1#

这段代码的问题是,在调用

$this->forge->createTable('products');

它将重置查询对象,因此它将丢失对表的引用,并且不会找到您正在查找的特定字段。因此,像这样更改查询的顺序:

$this->forge->addForeignKey('categories_id', 'categories', 'id');
$this->forge->createTable('products');

相关问题