DBIC +as がないとカラムがずれる件について
http://back-paper.atsushi.labs.mfac.jp/show?paper_rid=hPu9MWCKGd
実際にわたしが遭遇したのは以下のようにprefetchをつかって 結合先のテーブルのカラムをとったときにずれるという現象です。
my $friends = $schema->resultset('Friend')->search( { 'me.seq' => 12, }, { '+select' => [ \ 'RAND() as rand' ], # '+as' => [ 'rand' ], prefetch => 'profile', cache => 1, order_by => 'rand', page => 1, rows => 3 } ); while(my $friend = $friends->next){ print("------------"); print("seq => ". $friend->profile->seq); print("f_name =>" . $friend->profile->f_name); print("l_name => ". $friend->profile->l_name); print("birthday =>" . $friend->profile->birthday); print("sex_id=>".$friend->profile->sex_id); print("pref_code =>".$friend->profile->pref_code); print("comment =>" .$friend->profile->comment); print("regist_date =>" . $friend->profile->regist_date); print("update_date =>" . $friend->profile->update_date); print("------------"); }
結果
------------ seq => 0.531619906920586 f_name =>1 l_name => 性1 birthday =>名1 sex_id => 1976-01-01 pref_code =>1 comment =>13 regist_date =>0 update_date =>2006-12-15 16:36:29 ------------ ------------ seq => 0.877319584486372 f_name =>2 l_name => 性2 birthday =>名2 sex_id => 1906-01-01 pref_code =>1 comment =>01 regist_date =>0 update_date =>2006-12-15 17:29:14 ------------
結合先のテーブル PROFILE
+----------------------+-------------+ | Field | Type | +----------------------+-------------+ | seq | bigint(20) | | f_name | varchar(36) | | l_name | varchar(36) | | birthday | date | | sex_id | tinyint(4) | | pref_code | varchar(2) | | comment | text | | regist_date | datetime | | update_date | datetime | +----------------------+-------------+
+asがないと
カラムの先頭(ここではseq)にrandの結果が入ってしまっている。