Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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));
like image 829
Shekhar Joshi Avatar asked Mar 17 '15 06:03

Shekhar Joshi


1 Answers

Here are 4 ways to do that. The first three work with create_table and the fourth one can be done when adding a field later.

$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)',
]);
like image 85
colonelclick Avatar answered Oct 18 '22 11:10

colonelclick