From: Peter Rabbitson Date: Mon, 10 Nov 2008 23:09:23 +0000 (+0000) Subject: belongs_to relationships are explicitly flagged by default, to aid the DBIC SQLT... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6bf6ba2f2c58514c065a95019cd23b10bd4770ff;p=dbsrgits%2FDBIx-Class-Historic.git belongs_to relationships are explicitly flagged by default, to aid the DBIC SQLT parser (by abraxxa) --- diff --git a/lib/DBIx/Class/Relationship/BelongsTo.pm b/lib/DBIx/Class/Relationship/BelongsTo.pm index 5756f2b..272b01b 100644 --- a/lib/DBIx/Class/Relationship/BelongsTo.pm +++ b/lib/DBIx/Class/Relationship/BelongsTo.pm @@ -9,6 +9,11 @@ use warnings; sub belongs_to { my ($class, $rel, $f_class, $cond, $attrs) = @_; + + # assume a foreign key contraint unless defined otherwise + $attrs->{is_foreign_key_constraint} = 1 + if not exists $attrs->{is_foreign_key_constraint}; + # no join condition or just a column name if (!ref $cond) { $class->ensure_class_loaded($f_class); diff --git a/lib/DBIx/Class/ResultSource.pm b/lib/DBIx/Class/ResultSource.pm index f3d6792..83ad7cc 100644 --- a/lib/DBIx/Class/ResultSource.pm +++ b/lib/DBIx/Class/ResultSource.pm @@ -843,7 +843,7 @@ sub resolve_condition { #warn "$self $k $for $v"; unless ($for->has_column_loaded($v)) { if ($for->in_storage) { - $self->throw_exception("Column ${v} not loaded on ${for} trying to reolve relationship"); + $self->throw_exception("Column ${v} not loaded on ${for} trying to resolve relationship"); } return $UNRESOLVABLE_CONDITION; } diff --git a/t/66relationship.t b/t/66relationship.t index 4506bda..81646fa 100644 --- a/t/66relationship.t +++ b/t/66relationship.t @@ -7,7 +7,7 @@ use DBICTest; my $schema = DBICTest->init_schema(); -plan tests => 67; +plan tests => 69; # has_a test my $cd = $schema->resultset("CD")->find(4); @@ -117,14 +117,11 @@ $cd = $artist->find_or_new_related( 'cds', { is( $cd->title, 'Greatest Hits 2: Louder Than Ever', 'find_or_new_related new record ok' ); ok( ! $cd->in_storage, 'find_or_new_related on a new record: not in_storage' ); -# print STDERR Data::Dumper::Dumper($cd->get_columns); -# $cd->result_source->schema->storage->debug(1); $cd->artist(undef); my $newartist = $cd->find_or_new_related( 'artist', { name => 'Random Boy Band Two', artistid => 200, } ); -# $cd->result_source->schema->storage->debug(0); is($newartist->name, 'Random Boy Band Two', 'find_or_new_related new artist record with id'); is($newartist->id, 200, 'find_or_new_related new artist id set'); @@ -263,3 +260,12 @@ eval { is ($@, '', 'Staged insertion successful'); ok($new_artist->in_storage, 'artist inserted'); ok($new_related_cd->in_storage, 'new_related_cd inserted'); + +# check if is_foreign_key_constraint attr is set +my $rs_normal = $schema->source('Track'); +my $relinfo = $rs_normal->relationship_info ('cd'); +cmp_ok($relinfo->{attrs}{is_foreign_key_constraint}, '==', 1, "is_foreign_key_constraint defined for belongs_to relationships."); + +my $rs_overridden = $schema->source('ForceForeign'); +my $relinfo_with_attr = $rs_overridden->relationship_info ('cd_3'); +cmp_ok($relinfo_with_attr->{attrs}{is_foreign_key_constraint}, '==', 0, "is_foreign_key_constraint defined for belongs_to relationships with attr."); diff --git a/t/lib/DBICTest/Schema/ForceForeign.pm b/t/lib/DBICTest/Schema/ForceForeign.pm index 149f759..82829b8 100644 --- a/t/lib/DBICTest/Schema/ForceForeign.pm +++ b/t/lib/DBICTest/Schema/ForceForeign.pm @@ -29,4 +29,13 @@ __PACKAGE__->might_have( }, ); +# Normally this would appear as a FK constraint +__PACKAGE__->belongs_to( + 'cd_3', 'DBICTest::Schema::CD', { + 'foreign.cdid' => 'self.cd', + }, { + is_foreign_key_constraint => 0, + }, +); + 1; diff --git a/t/lib/sqlite.sql b/t/lib/sqlite.sql index c4ff125..1db9557 100644 --- a/t/lib/sqlite.sql +++ b/t/lib/sqlite.sql @@ -1,6 +1,6 @@ -- -- Created by SQL::Translator::Producer::SQLite --- Created on Sun Nov 9 16:28:17 2008 +-- Created on Mon Nov 10 23:52:55 2008 -- BEGIN TRANSACTION; @@ -190,6 +190,7 @@ CREATE TABLE liner_notes ( notes varchar(100) NOT NULL ); +CREATE INDEX liner_notes_idx_liner_id_liner ON liner_notes (liner_id); -- -- Table: link