[Dbix-class] Prefetch Problem: Related data can't be fetched

Jess Robinson castaway at desert-island.me.uk
Tue Feb 26 13:35:54 GMT 2008


On Tue, 26 Feb 2008, Matt Lawrence wrote:

> sindharta_tanuwijaya at yahoo.co.jp wrote:
>> Hi,
>>
>> I am having a problem, in which I couldn't get the relationship column
>> data by using prefetch.
>> My relationship is declared like this, suppose we have two tables:
>> railway and railway_station, which relationship is 1 to many.
>>
>> #in railway schema
>> __PACKAGE__->has_many(
>> "railway_station",
>> "Schema::RailwayStation",
>> { "foreign.railway_station_railway_id" => "self.railway_id" },
>> );
>>
>> #in railway_station schema
>> __PACKAGE__->belongs_to(
>> "railway",
>> "Schema::Railway",
>> { "foreign.railway_id" => "self.railway_station_railway_id" },
>> );
>>
>> My prefetch command is as follows:
>>
>> my $railways = $schema->resultset('Railway')->search(
>> {
>> 'me.railway_id' => { 'IN' => $selected_railway_ids },
>> },
>> {
>> join => 'railway_station',
>> prefetch => 'railway_station',
>> }
>> );
>>
> railway_station is a has_many relationship, so you can't prefetch it.
> You could do it the other way around and prefetch "railway" from a
> RailwayStation resultset however:

Rubbish, whatever gave you that idea? You can indeed prefetch a has_many 
rel.

Jess



More information about the DBIx-Class mailing list