Shekhar Joshi
Shekhar Joshi

Reputation: 1008

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));

Upvotes: 5

Views: 8963

Answers (2)

Abdul Manan
Abdul Manan

Reputation: 2375

Use $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);

Upvotes: 3

colonelclick
colonelclick

Reputation: 2215

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)',
]);

Upvotes: 11

Related Questions