CodeIgniter Forums
Add Primary column with dbforge library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Add Primary column with dbforge library (/showthread.php?tid=1158)



Add Primary column with dbforge library - 50l3r - 02-15-2015

I want to add primary columns in existent tables:

PHP Code:
$this->dbforge->add_column('name_table',array("fieldname" => array('type' => 'BIGINT(20)''auto_increment'=> true'null' => false'first' => TRUE));
$this->dbforge->add_key('fieldname'true); 

I tried adding key separately but no results. I recieve this error

Code:
Error Number: 1075

Incorrect table definition; there can be only one auto column and it must be defined as a key

How can i add primary columns to a existent table?


RE: Add Primary column with dbforge library - CroNiX - 02-16-2015

It's just what the mysql error says: you can only have one AUTO INCREMENTING column per table. You apparently already have one, and are trying to add a second. So turn auto_increment to FALSE.


RE: Add Primary column with dbforge library - 50l3r - 02-16-2015

I dont set any autoincrement columns. This is the table structure:

CREATE TABLE IF NOT EXISTS `empresas` (
`nombre` varchar(1000) NOT NULL,
`_EntityCreateDate` datetime NOT NULL,
`_EntityUpdateDate` datetime NOT NULL,
`_EntityCreateUser` bigint(20) NOT NULL,
`_EntityUpdateUser` bigint(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


RE: Add Primary column with dbforge library - mwhitney - 02-17-2015

Did you try calling add_key() first, then add_column()? The main reason I ask is that when creating a table, add_key() and add_field() just modify the state of dbforge, while create_table does the actual SQL generation and execution. add_column() also generates and executes the SQL, so add_key() would have to be called before it to have any chance to work properly. Unfortunately, without testing, I don't know if add_column() will actually add a primary key, though.

If that doesn't work, you may have to create a new table and copy the data over.