PostgreSQL specific problem with relational query


I’m new to Yii and am struggling with a problem that I can’t seem to solve. I have made a small example database to illustrate the problem, it looks like this:



[size=“4”]What I’m trying to do[/size]

I’m trying to list all comments where the user has null in its’ name-field and pass it to a CListView-widget.

[size=“4”]How I’m trying to do it[/size]

// comments-model relations

public function relations()


	return array(

		'user' => array(self::BELONGS_TO, 'Users', 'user_id'),



// comments-controller function

public function actionIndex()


	$dataProvider=new CActiveDataProvider('Comments', array(


			'condition'=>' IS NULL',

			'order'=>' DESC',








// comments/index-view function

<?php $this->widget('zii.widgets.CListView', array(



)); ?>

[size="4"]What is going wrong[/size]

With MySQL it works just fine and I get a list of my comments with NULL-named users.

However, if I switch to PostgreSQL I get the following error message:


CDbCommand failed to execute the SQL statement: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "."

LINE 1: ..."user" ON ("t"."user_id"="user"."id") WHERE ( IS N...


The SQL statement executed was: SELECT COUNT(DISTINCT "t"."id") FROM "comments" "t" LEFT OUTER JOIN "users" "user" ON ("t"."user_id"="user"."id") WHERE ( IS NULL)

If anyone can make sense of this and point me in the right direction I would very much appreciate it!

[size="4"]My setup[/size]

Yii 1.1.7

PostgreSQL 8.4.8

MySQL 5.1.54

Apache 2.2.17

I’m not a PostgreSQL expert, but “user” is a reserved keyword in pg AFAIK. Try to quote it when used as a table name.

Thank you for the suggestion!

I might have used unfortunate names in my example database, however I get the same kind of error in a database where the tables are storing items and shipments instead of users and comments. So I think that there is something more that is causing trouble here.