[Dbix-class] update circular reference
shawn wilson
ag4ve.us at gmail.com
Fri Apr 12 20:26:55 GMT 2013
How do I maintain a circular reference? The relation part of the
schema is below, but I can do:
my $url_rs = $self->search({url => $url})->single;
foreach my $text (@{$data->{$domain}{$url}})
{
try {
$schema->txn_do(sub {
$url_rs->find_or_create_related('data', {
text => $text,
stamp => $eptime,
source => {
proxy => $info->{proxy},
ip => $info->{ip} // '0.0.0.0',
},
});
});
} catch {
print STDERR "[$text] $_\n";
die;
};
}
}
Where I can go from page and insert data and source. But, how do I
fill in the source_fk in the page table that I orriginate from?
# Page
__PACKAGE__->add_columns(
'page_pk',
{
data_type => 'integer',
extra => { unsigned => 1 },
is_auto_increment => 1,
is_nullable => 0,
},
'source_fk',
{
data_type => 'integer',
extra => { unsigned => 1 },
is_foreign_key => 1,
is_nullable => 1,
},
);
__PACKAGE__->has_many(
'data',
'WebDat::Schema::Result::Data',
{ 'foreign.page_fk' => 'self.page_pk' },
{ is_deferrable => 1 },
);
__PACKAGE__->belongs_to(
'source' =>
'WebDat::Schema::Result::Source',
{ 'foreign.source_pk' => 'self.source_fk' },
{ is_deferrable => 1 },
);
1;
# Source
__PACKAGE__->add_columns(
'source_pk',
{
data_type => 'integer',
extra => { unsigned => 1 },
is_auto_increment => 1,
is_nullable => 0,
},
);
__PACKAGE__->has_many(
'data',
'WebDat::Schema::Result::Data',
{ 'foreign.source_fk' => 'self.source_pk' },
{ is_deferrable => 1 },
);
__PACKAGE__->has_many(
'page',
'WebDat::Schema::Result::Page',
{ 'foreign.source_fk' => 'self.source_pk' },
{ is_deferrable => 1 },
);
1;
# Data
__PACKAGE__->add_columns(
'data_pk',
{
data_type => 'integer',
extra => { unsigned => 1 },
is_auto_increment => 1,
is_nullable => 0,
},
'page_fk',
{
data_type => 'integer',
extra => { unsigned => 1 },
is_foreign_key => 1,
is_nullable => 0,
},
'source_fk',
{
data_type => 'integer',
extra => { unsigned => 1 },
is_foreign_key => 1,
is_nullable => 0,
},
);
__PACKAGE__->belongs_to(
'page' =>
'WebDat::Schema::Result::Page',
{ 'foreign.page_pk' => 'self.page_fk' },
{ is_deferrable => 1 },
);
__PACKAGE__->belongs_to(
'source',
'WebDat::Schema::Result::Source',
{ 'foreign.source_pk' => 'self.source_fk' },
{ is_deferrable => 1 },
);
1;
More information about the DBIx-Class
mailing list