Welcome Guest, Not a member yet? Register   Sign In
Add Primary column with dbforge library
#1
Question 

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?
Reply
#2

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.
Reply
#3

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;
Reply
#4

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB