10 my $schema = DBICTest->init_schema();
12 my $artist = $schema->resultset ('Artist')->find(1);
14 my $genre = $schema->resultset ('Genre')
15 ->create ({ name => 'par excellence' });
16 my $genre_cds = $genre->cds;
18 is ($genre_cds->count, 0, 'No cds yet');
21 $genre->update_or_create_related ('cds', {
24 title => 'the best thing since sliced bread',
27 # verify cd was inserted ok
28 is ($genre_cds->count, 1, 'One cd');
29 my $cd = $genre_cds->first;
31 { map { $_, $cd->get_column ($_) } qw/artist year title/ },
33 artist => $artist->id,
35 title => 'the best thing since sliced bread',
37 'CD created correctly',
40 # expect a year update on the only related row
41 # (non-qunique column + unique column set as disambiguator)
42 $genre->update_or_create_related ('cds', {
44 title => 'the best thing since sliced bread',
48 # re-fetch the cd, verify update
49 is ($genre->search_related( 'cds' )->count, 1, 'Still one cd');
50 $cd = $genre_cds->first;
52 { map { $_, $cd->get_column ($_) } qw/artist year title/ },
54 artist => $artist->id,
56 title => 'the best thing since sliced bread',
58 'CD year column updated correctly',
61 # expect a failing create:
62 # the unique constraint is not complete, and there is nothing
63 # in the database with such a year yet - insertion will fail due
64 # to missing artist fk
66 $genre->update_or_create_related ('cds', {
68 title => 'the best thing since sliced bread',
70 } qr/DBI Exception.+(?x:
71 \QNOT NULL constraint failed: cd.artist\E
73 \Qcd.artist may not be NULL\E
74 )/s, 'ambiguous find + create failed'
77 # expect a create, after a failed search using *only* the
78 # *current* relationship and the unique column constraints
80 $schema->is_executed_sql_bind( sub {
81 $genre->update_or_create_related ('cds', {
82 title => 'the best thing since vertical toasters',
88 'SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
90 WHERE ( me.artist = ? AND me.genreid = ? AND me.title = ? )
94 "the best thing since vertical toasters",
97 'INSERT INTO cd ( artist, genreid, title, year) VALUES ( ?, ?, ?, ? )',
100 "the best thing since vertical toasters",
103 ], 'expected select issued' );
105 # a has_many search without a unique constraint makes no sense
106 # but I am not sure what to test for - leaving open