From 35554bc837e77eae182b07f98169b7e4f4d3d3fc Mon Sep 17 00:00:00 2001 From: Andrew Gregory Date: Sat, 27 Jun 2015 11:32:01 -0400 Subject: [PATCH] Schema/Field: add foreign_key_reference builder foreign_key_reference was only being set from the is_foreign_key builder. If a user manually set is_foreign_key, as DBIx::Class does, foreign_key_reference would never be set, breaking the HTML producer. --- lib/SQL/Translator/Schema/Field.pm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/SQL/Translator/Schema/Field.pm b/lib/SQL/Translator/Schema/Field.pm index a88263233..cd4bc2ca7 100644 --- a/lib/SQL/Translator/Schema/Field.pm +++ b/lib/SQL/Translator/Schema/Field.pm @@ -180,6 +180,7 @@ has foreign_key_reference => ( is => 'rw', predicate => '_has_foreign_key_reference', isa => schema_obj('Constraint'), + builder => 1, weak_ref => 1, ); @@ -197,6 +198,22 @@ around foreign_key_reference => sub { $self->$orig; }; +sub _build_foreign_key_reference { + my ( $self ) = @_; + + if ( my $table = $self->table ) { + for my $c ( $table->get_constraints ) { + if ( $c->type eq FOREIGN_KEY ) { + my %fields = map { $_, 1 } $c->fields; + if ( $fields{ $self->name } ) { + return $c; + } + } + } + } + return undef; +} + =head2 is_auto_increment Get or set the field's C attribute.