gpt4 book ai didi

php - 在 Codeigniter 迁移中添加外键

转载 作者:IT王子 更新时间:2023-10-29 00:33:31 25 4
gpt4 key购买 nike

有人可以帮我添加外键并在 Codeigniter 迁移数据库中吗?这是代码:

public function up()
{
$this->dbforge->add_field(array(
'ID_PELAYANAN' => array(
'type' => 'INT',
'constraint' => 50,
'auto_increment' => TRUE
),

'THBLMUT' => array(
'type' => 'VARCHAR',
'constraint' => '6',
),

'ID_DIST' => array(
'type' => 'VARCHAR',
'constraint' => '2',
),

'ID_AREA' => array(
'type' => 'VARCHAR',
'constraint' => '5',
),

'ID_RAYON' => array(
'type' => 'VARCHAR',
'constraint' => '5',
),

'N_RAYON' => array(
'type' => 'CHAR',
'constraint' => '70',
),

));
$this->dbforge->add_key('ID_PELAYANAN', TRUE);
$this->dbforge->create_table('pelayanan');
}
public function down()
{
$this->dbforge->drop_table('pelayanan');
}
}

我想在此表中将 'ID_AREA'、'ID_RAYON' 作为 Forein Key。我该如何解决这个问题?

最佳答案

这里有两种方法可以做到这一点。第一个适用于 create_table,另一个可以在向现有表添加字段时完成。

$this->dbforge->add_field('CONSTRAINT FOREIGN KEY (id) REFERENCES table(id)');

$this->dbforge->add_column('table',[
'CONSTRAINT fk_id FOREIGN KEY(id) REFERENCES table(id)',
]);

关于php - 在 Codeigniter 迁移中添加外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31226971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com