Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema/Field: add foreign_key_reference builder #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/SQL/Translator/Schema/Field.pm
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,10 @@ Get or set the field's foreign key reference;
has foreign_key_reference => (
is => 'rw',
predicate => '_has_foreign_key_reference',
isa => schema_obj('Constraint'),
isa => sub { not defined $_[0] or schema_obj('Constraint')->(@_) },
builder => 1,
weak_ref => 1,
lazy => 1,
);

around foreign_key_reference => sub {
Expand All @@ -197,6 +199,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<is_auto_increment> attribute.
Expand Down