Reputation: 83
I have a table 'forms', that has a primary key - 'id'. This primary key was created via migration:
$this->createTable('forms', [
'id' => $this->primaryKey(),
'group_id' => $this->integer(11),
'form_id' => $this->integer(11)
]);
Now I need to delete this primary key 'id' and create a new composite primary key from 'form_id' and 'group_id'.
I have tried something like this in new migration:
public function safeUp()
{
$this->dropPrimaryKey('id', 'forms');
$this->addPrimaryKey('pk_forms', 'forms', ['form_id', 'group_id']);
}
But the error occurs: drop primary key id ...Exception: SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
How can I delete old primary key and create a new one?
Upvotes: 0
Views: 1436
Reputation: 219
That error means your primary key is an auto incrementing column, such columns must be a key as the error says, so in order to replace it you would need to first remove the auto_increment behavior
I haven't actually worked with yii migrations, but this might work (or ... it might not)
$this->alterColumn('forms', 'id', 'integer');
$this->dropPrimaryKey('id', 'forms');
$this->addPrimaryKey('pk_forms', 'forms', ['form_id', 'group_id']);
Upvotes: 1