使用或迁移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\Exceptions\DatabaseException类型:
消息:字段
categories_id未找到.
发布于 2020-06-13 08:00:37
此代码的问题是在调用
$this->forge->createTable('products');它将重置查询对象,因此它将失去对表的引用,而不会找到您要查找的特定字段。因此,请按如下方式更改查询顺序:
$this->forge->addForeignKey('categories_id', 'categories', 'id');
$this->forge->createTable('products');https://stackoverflow.com/questions/62353435
复制相似问题