frioux / DBIx-Class-Helpers

https://metacpan.org/pod/DBIx::Class::Helpers
20 stars 38 forks source link

OnColumnMissing breaks create with related objects #93

Open andrewgregory opened 6 years ago

andrewgregory commented 6 years ago
#!/usr/bin/perl

use strict;
use warnings;

use Test::More;

my $schema = TestSchema->connect('dbi:SQLite:dbname=:memory:');
$schema->deploy;
my $rs = $schema->resultset('Foo');

# breaks Helper::Row::OnColumnMissing
ok( eval { $rs->create( { id => 1, bar => [ { id => 1 } ] } ); 1; } );
is "$@", '';
is( $rs->find( { id => 1 } )->bar->count, 1 );

done_testing;

BEGIN {
    package TestSchema::Bar;
    use parent 'DBIx::Class::Core';

    # breaks creating related Bar from Foo
    __PACKAGE__->load_components('Helper::Row::OnColumnMissing');
    sub on_column_missing {'die' }

    __PACKAGE__->table('bar');
    __PACKAGE__->add_columns(
        id     => { data_type => 'integer' },
        foo_id => { data_type => 'integer' },
    );
    __PACKAGE__->set_primary_key('id');

    package TestSchema::Foo;
    use parent 'DBIx::Class::Core';
    __PACKAGE__->table('foo');
    __PACKAGE__->add_columns( id => { data_type => 'integer' }, );
    __PACKAGE__->set_primary_key('id');

    package TestSchema;
    use parent 'DBIx::Class::Schema';
    # create relationships after results are fully defined
    TestSchema::Bar->belongs_to( foo => 'TestSchema::Foo', 'foo_id' );
    TestSchema::Foo->has_many( bar => 'TestSchema::Bar', 'foo_id' );
    __PACKAGE__->register_class( Foo => __PACKAGE__ . '::Foo' );
    __PACKAGE__->register_class( Bar => __PACKAGE__ . '::Bar' );
}