首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >伪造迁移CodeIgniter 4外键错误

伪造迁移CodeIgniter 4外键错误
EN

Stack Overflow用户
提问于 2020-06-12 22:37:03
回答 1查看 3.4K关注 0票数 1

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

迁移产品

代码语言:javascript
复制
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');

}

迁移类别

代码语言:javascript
复制
$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\Exceptions\DatabaseException类型:

消息:字段categories_id未找到.

EN

回答 1

Stack Overflow用户

发布于 2020-06-13 08:00:37

此代码的问题是在调用

代码语言:javascript
复制
$this->forge->createTable('products');

它将重置查询对象,因此它将失去对表的引用,而不会找到您要查找的特定字段。因此,请按如下方式更改查询顺序:

代码语言:javascript
复制
$this->forge->addForeignKey('categories_id', 'categories', 'id');
$this->forge->createTable('products');
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62353435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档