Skip to content

Instantly share code, notes, and snippets.

@ilmari
Last active December 11, 2015 05:59
Show Gist options
  • Save ilmari/4556223 to your computer and use it in GitHub Desktop.
Save ilmari/4556223 to your computer and use it in GitHub Desktop.
package MySchema::Result::Bar;
use strict;
use warnings;
use parent qw(DBIx::Class::Core);
__PACKAGE__->table("bar");
__PACKAGE__->add_columns(
id => {
data_type => "serial",
is_auto_increment => 1,
is_nullable => 0,
},
product_id => {
data_type => "integer",
is_nullable => 0,
}
);
__PACKAGE__->set_primary_key(qw(id));
1;
package MySchema::Result::Base;
use strict;
use warnings;
use parent qw(DBIx::Class::Core);
__PACKAGE__->table("base");
__PACKAGE__->add_columns(
"id", {
data_type => "serial",
is_auto_increment => 1,
is_nullable => 0,
sequence => 'product_data_id_seq',
},
);
__PACKAGE__->set_primary_key(qw(id));
__PACKAGE__->has_many(foo => 'MySchema::Result::Foo',
{ 'foreign.base_id' => 'self.id' });
1;
package MySchema::Result::Foo;
use strict;
use warnings;
use parent qw(DBIx::Class::Core);
__PACKAGE__->table("foo");
__PACKAGE__->add_columns(
"id", {
data_type => "serial",
is_auto_increment => 1,
is_nullable => 0,
# sequence => 'product_data_id_seq',
},
base_id => {
data_type => "integer",
is_nullable => 0,
},
);
__PACKAGE__->set_primary_key(qw(id));
__PACKAGE__->has_many('bar' =>
'MySchema::Result::Bar', {'foreign.product_id' => 'self.id'}, { cascade_copy => 1, cascade_delete => 1}
);
1;
package MySchema;
use strict;
use warnings;
use parent qw(DBIx::Class::Schema);
__PACKAGE__->load_namespaces;
1;
$ use MySchema
$ my $schema = MySchema->connect("dbi:Pg:")
$MySchema1 = MySchema=HASH(0x3c80b20);
$ my $base = $schema->resultset("Base")->create({})
$MySchema_Result_Base1 = MySchema::Result::Base=HASH(0x3a8e7c8);
$ $base->id
1
$ my $foo = $base->create_related('foo', {})
$MySchema_Result_Foo1 = MySchema::Result::Foo=HASH(0x42b2068);
$ $foo->base_id
1
$ $foo->bar->create({ });
$MySchema_Result_Bar1 = MySchema::Result::Bar=HASH(0x3f17db8);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment