2 Primary Keys


I’ve been trying to use gii to create a model for a table with 2 primary keys but it says it is not supported. How come? Is there any work around to this?

I’m assuming you are asking about support in Yii, and not how to do this in SQL.

To use a composite primary key in ActiveRecord, define the following method in your classes (or base class):

public function primaryKey()


    return array('pk1','pk2');


I’m not sure if this is generated by Yii if you are creating your classes that way.

You can remove the super key,

create another primary key with another field,

create model with gii,

remove the primary key,

follow the eirikhm suggestion.

I got the same problem and I solved in this way: gii is not able to create a crud with a super key. But Yii can work with a super key.