简体   繁体   English

如何使用dbforge [codeigniter]添加外键

[英]How to use dbforge[of codeigniter] to add foreign key

I am using codeigniter and I am trying to convert following query into dbforge style query, how can I do it? 我正在使用codeigniter而我正在尝试将以下查询转换为dbforge样式查询,我该怎么办?

create table filter (
    filterid int primary key auto_increment,
    filtername varchar(50) not null,
    categoryid int not null,
    isactive tinyint not null,
    sequence int not null,
    foreign key(categoryid) references category(id));

Here are 4 ways to do that. 以下是4种方法。 The first three work with create_table and the fourth one can be done when adding a field later. 前三个使用create_table和第四个可以在以后添加字段时完成。

$this->dbforge->add_field('id INT NOT NULL AUTO_INCREMENT PRIMARY KEY');

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

$this->dbforge->add_field('INDEX (deleted)');

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

Use $this->db->query(); 使用$this->db->query(); instead. 代替。

This always works for me.... 这总是对我有用....

$sql="create table filter (
filterid int primary key auto_increment,
filtername varchar(50) not null,
categoryid int not null,
isactive tinyint not null,
sequence int not null,
foreign key(categoryid) references category(id))";

$this->db->query($sql);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM