From: Justin Hunter Date: Sun, 18 Oct 2009 06:11:13 +0000 (-0700) Subject: if primary key constraint is added, make associated columns primary keys X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=aa20eb7f0fbf686dab4347bd967f6990f1d5e3f6;p=dbsrgits%2FSQL-Translator-2.0-ish.git if primary key constraint is added, make associated columns primary keys --- diff --git a/lib/SQL/Translator/Object/Constraint.pm b/lib/SQL/Translator/Object/Constraint.pm index 9a9b43c..5f99592 100644 --- a/lib/SQL/Translator/Object/Constraint.pm +++ b/lib/SQL/Translator/Object/Constraint.pm @@ -35,6 +35,7 @@ class SQL::Translator::Object::Constraint extends SQL::Translator::Object { has 'type' => ( is => 'rw', isa => Str, + predicate => 'has_type', ); has 'deferrable' => ( @@ -70,5 +71,12 @@ class SQL::Translator::Object::Constraint extends SQL::Translator::Object { default => '' ); - around add_column(Column $column) { $self->$orig($column->name, $column) } + around add_column(Column $column) { + if ($self->has_type && $self->type eq 'PRIMARY KEY') { + $column->is_primary_key(1); + } + $self->$orig($column->name, $column) + } + + method is_valid { return $self->has_type && scalar $self->column_ids ? 1 : undef } } diff --git a/lib/SQL/Translator/Object/Table.pm b/lib/SQL/Translator/Object/Table.pm index 444359c..6131ff9 100644 --- a/lib/SQL/Translator/Object/Table.pm +++ b/lib/SQL/Translator/Object/Table.pm @@ -104,6 +104,9 @@ class SQL::Translator::Object::Table extends SQL::Translator::Object is dirty { $name = 'ANON' . $idx; } $constraint->table($self); + if ($constraint->has_type && $constraint->type eq 'PRIMARY KEY') { + $self->get_column($_)->is_primary_key(1) for $constraint->column_ids; + } $self->$orig($name, $constraint) }