8 my $schema = DBICTest->init_schema();
12 is_deeply([ sort $schema->source('CD')->unique_constraint_names ], [ qw/cd_artist_title primary/ ], 'CD source has an automatically named unique constraint');
13 is_deeply([ sort $schema->source('Producer')->unique_constraint_names ], [ qw/primary prod_name/ ], 'Producer source has a named unique constraint');
16 my $title = 'UNIQUE Constraint';
18 my $cd1 = $schema->resultset('CD')->find_or_create({
24 my $cd2 = $schema->resultset('CD')->find(
29 { key => 'cd_artist_title' }
32 is($cd2->get_column('artist'), $cd1->get_column('artist'), 'find by specific key: artist is correct');
33 is($cd2->title, $cd1->title, 'title is correct');
34 is($cd2->year, $cd1->year, 'year is correct');
36 my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'cd_artist_title' });
38 is($cd3->get_column('artist'), $cd1->get_column('artist'), 'find by specific key, ordered columns: artist is correct');
39 is($cd3->title, $cd1->title, 'title is correct');
40 is($cd3->year, $cd1->year, 'year is correct');
42 my $cd4 = $schema->resultset('CD')->update_or_create(
50 ok(! $cd4->is_changed, 'update_or_create without key: row is clean');
51 is($cd4->cdid, $cd2->cdid, 'cdid is correct');
52 is($cd4->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
53 is($cd4->title, $cd2->title, 'title is correct');
54 is($cd4->year, 2007, 'updated year is correct');
56 my $cd5 = $schema->resultset('CD')->update_or_create(
62 { key => 'cd_artist_title' }
65 ok(! $cd5->is_changed, 'update_or_create by specific key: row is clean');
66 is($cd5->cdid, $cd2->cdid, 'cdid is correct');
67 is($cd5->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
68 is($cd5->title, $cd2->title, 'title is correct');
69 is($cd5->year, 2007, 'updated year is correct');
71 my $cd6 = $schema->resultset('CD')->update_or_create(
81 ok(! $cd6->is_changed, 'update_or_create by PK: row is clean');
82 is($cd6->cdid, $cd2->cdid, 'cdid is correct');
83 is($cd6->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
84 is($cd6->title, $cd2->title, 'title is correct');
85 is($cd6->year, 2005, 'updated year is correct');
87 my $cd7 = $schema->resultset('CD')->find_or_create(
93 { key => 'cd_artist_title' }
96 is($cd7->cdid, $cd1->cdid, 'find_or_create by specific key: cdid is correct');
97 is($cd7->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
98 is($cd7->title, $cd1->title, 'title is correct');
99 is($cd7->year, $cd1->year, 'year is correct');
101 my $artist = $schema->resultset('Artist')->find($artistid);
102 my $cd8 = $artist->find_or_create_related('cds',
107 { key => 'cd_artist_title' }
110 is($cd8->cdid, $cd1->cdid, 'find_or_create related by specific key: cdid is correct');
111 is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
112 is($cd8->title, $cd1->title, 'title is correct');
113 is($cd8->year, $cd1->year, 'year is correct');
115 my $cd9 = $artist->update_or_create_related('cds',
120 { key => 'cd_artist_title' }
123 ok(! $cd9->is_changed, 'update_or_create by specific key: row is clean');
124 is($cd9->cdid, $cd1->cdid, 'cdid is correct');
125 is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
126 is($cd9->title, $cd1->title, 'title is correct');
127 is($cd9->year, 2021, 'year is correct');