Multiple databases, one postgresql and problem with gii

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)




Any ideas what I am doing wrong?

If you have followed conventions and used ‘id’ as primary key, maybe?

It’s normally foreign keys which has tablename_id as name…

You could try that.;)

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?

Implement primaryKey() method in FoobarAccount:




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.

Much better answer, Phtamas - PrimaryKey() is the way to go if you can’t change the schema. :)

Thanks, this method (function primaryKey) seems to be working ok.

Ok, I spoke too soon. When I try to go to view a Foobaraccount entry, it errors out with "Property "Foobaraccount.id" is not defined.

Can you post the code?

He just have to change ->id to ->account_id in the code. :)

Maybe this is a slight bug in Gii?

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:




<?php

$this->breadcrumbs=array(

    'Foobaraccounts'=>array('index'),

    $model->id,

);



So it didn’t get the right primary key field name for the breadcrumbs part. But this is Yii 1.1.4 so maybe its already been fixed.