I have two databases, one that was setup for my Yii application that uses Mysql and follows Yii style conventions for naming and another one that was setup without Yii in mind that uses Postgresql. One of the issues that I face is that I have tables named ‘account’ in both databases, but for different purposes. To remedy this name clash, I was going to make the model for the postgresql database account table called Foobaraccount and tied it to table account. That works ok and Gii creates the model file. But when I go into the Gii crud generator and try to generate there using Foobaraccount as the model class, it errors out with “Table ‘account’ does not have a primary key.” Here is the schema for the account table on the postgresql database:
# \d account;
Table "public.account"
Column | Type | Modifiers
-------------------+-----------------------------+--------------------------------------------------------------
account_id | integer | not null default nextval('account_account_id_seq'::regclass)
username | character varying(255) |
password | character varying(255) |
created | timestamp without time zone |
modified | timestamp without time zone |
Indexes:
"account_account_id_key" UNIQUE, btree (account_id)
Foreign-key constraints:
"account_account_status_id_fkey" FOREIGN KEY (account_status_id) REFERENCES account_status(account_status_id)
No, the schema of the account table in the postgres database is as shown. The primary key is account_id, not id. Do I have to adjust something in the FoobarAccount model file or somewhere else?
public function primaryKey()
{
return 'account_id';
}
Or (better): define account_id column as PRIMARY KEY instead of UNIQUE NOT NULL. Technically the same, but Yii probably looks for an explicit PRIMARY KEY constraint when loading metadata from the database.
Perhaps so, I was just about to say that its already referenced as account_id, but then I noticed where the error was really being generated from. At the top of the view.php was this that was generated by the Gii module: