Different SQL generated while on production server

Hi,

I’ve deployed my application to the production server today. Everything went good with a small exception - ActiveRecord generated slightly different SQL for relations in some modules.

Servers are 1:1 machines - same hardware, same software. I have same database structure and data on both servers too. But there’s this:

I have module that handles pages and their localizations. Both page and localization have a relation to User model, identified by user_id. On development, this works like a charm and generates


...FROM "users" "author" WHERE ("author"."[b]id[/b]"=:ypl0)

which is the right way and returns what it’s supposed to.

But when on production, it generates


...FROM "users" "author" WHERE ("author"."[b]username[/b]"=:ypl0)

instead and I’m unable to find a reason for this behavior. As I said, everything is copy 1:1.

The relation is defined by:


'author' => array(

	self::BELONGS_TO,

	'User',

	'user_id',

	'together' => true

)

in the right place. I even tried to add the ‘together’ option but there’s no change. Maybe the solution to this is pretty simple but I just don’t see it right now so I’m glad for any advice you might provide…

Thanks

UPDATE: I might have found why it does that but still don’t know why it behaves like this only on production. ‘username’ is part of the primary key - same on both development and production but it behaves strange only on production.

what is the code that generates that SQL?


$pages = Page::model()->findAll('site_id = :site_id', array(

	'site_id' => Yii::app()->params['siteId']

));

Just this. This gets all pages for specific website. Each page have relations:


public function relations()

{

	return array(

		'author' => array( // Page author

			self::BELONGS_TO,

			'User',

			'user_id'

		),

		'localizations' => array( // Page localizations

			self::HAS_MANY,

			'PageLocale',

			'page_id'

		),

	);

}

Template (HTML stripped):


foreach ($pages as $p) {

	echo $p->author->username;

}

Drops the error. Model User have it’s pkey set to ‘id’. In database there was also ‘username’ in pkey. I removed that, leaving only ‘id’ and problem is solved. Strange thing is that it happened only in production environment and not in development. Clearly, it was my fault but I’d like to find out why it happened only on prod.