7 my $title = 'UNIQUE Constraint';
9 my $cd1 = $schema->resultset('CD')->find_or_create({
15 my $cd2 = $schema->resultset('CD')->find(
20 { key => 'artist_title' }
23 is($cd2->get_column('artist'), $cd1->get_column('artist'), 'find by specific key: artist is correct');
24 is($cd2->title, $cd1->title, 'title is correct');
25 is($cd2->year, $cd1->year, 'year is correct');
27 my $cd3 = $schema->resultset('CD')->find($artistid, $title, { key => 'artist_title' });
29 is($cd3->get_column('artist'), $cd1->get_column('artist'), 'find by specific key, ordered columns: artist is correct');
30 is($cd3->title, $cd1->title, 'title is correct');
31 is($cd3->year, $cd1->year, 'year is correct');
33 my $cd4 = $schema->resultset('CD')->update_or_create(
41 ok(! $cd4->is_changed, 'update_or_create without key: row is clean');
42 is($cd4->cdid, $cd2->cdid, 'cdid is correct');
43 is($cd4->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
44 is($cd4->title, $cd2->title, 'title is correct');
45 is($cd4->year, 2007, 'updated year is correct');
47 my $cd5 = $schema->resultset('CD')->update_or_create(
53 { key => 'artist_title' }
56 ok(! $cd5->is_changed, 'update_or_create by specific key: row is clean');
57 is($cd5->cdid, $cd2->cdid, 'cdid is correct');
58 is($cd5->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
59 is($cd5->title, $cd2->title, 'title is correct');
60 is($cd5->year, 2007, 'updated year is correct');
62 my $cd6 = $schema->resultset('CD')->update_or_create(
72 ok(! $cd6->is_changed, 'update_or_create by PK: row is clean');
73 is($cd6->cdid, $cd2->cdid, 'cdid is correct');
74 is($cd6->get_column('artist'), $cd2->get_column('artist'), 'artist is correct');
75 is($cd6->title, $cd2->title, 'title is correct');
76 is($cd6->year, 2005, 'updated year is correct');
78 my $cd7 = $schema->resultset('CD')->find_or_create(
84 { key => 'artist_title' }
87 is($cd7->cdid, $cd1->cdid, 'find_or_create by specific key: cdid is correct');
88 is($cd7->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
89 is($cd7->title, $cd1->title, 'title is correct');
90 is($cd7->year, $cd1->year, 'year is correct');
92 my $artist = $schema->resultset('Artist')->find($artistid);
93 my $cd8 = $artist->find_or_create_related('cds',
99 { key => 'artist_title' }
102 is($cd8->cdid, $cd1->cdid, 'find_or_create related by specific key: cdid is correct');
103 is($cd8->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
104 is($cd8->title, $cd1->title, 'title is correct');
105 is($cd8->year, $cd1->year, 'year is correct');
107 my $cd9 = $artist->update_or_create_related('cds',
113 { key => 'artist_title' }
116 ok(! $cd9->is_changed, 'update_or_create by specific key: row is clean');
117 is($cd9->cdid, $cd1->cdid, 'cdid is correct');
118 is($cd9->get_column('artist'), $cd1->get_column('artist'), 'artist is correct');
119 is($cd9->title, $cd1->title, 'title is correct');
120 is($cd9->year, 2021, 'year is correct');