diff --git a/lib/SQL/Translator/Producer/MySQL.pm b/lib/SQL/Translator/Producer/MySQL.pm index 904b294c3..7aaeb1a74 100644 --- a/lib/SQL/Translator/Producer/MySQL.pm +++ b/lib/SQL/Translator/Producer/MySQL.pm @@ -449,7 +449,8 @@ sub create_table my %indexed_fields; for my $index ( $table->get_indices ) { push @index_defs, create_index($index, $options); - $indexed_fields{ $_ } = 1 for $index->fields; + $indexed_fields{ join "\0", $index->fields } = 1; + $indexed_fields{ ($index->fields)[0] } = 1; } # @@ -457,13 +458,21 @@ sub create_table # my @constraint_defs; my @constraints = $table->get_constraints; + + for my $pk ( grep $_->type eq PRIMARY_KEY, @constraints ) { + $indexed_fields{ join "\0", $pk->fields } = 1; + $indexed_fields{ ($pk->fields)[0] } = 1; + } + + for my $c ( @constraints ) { my $constr = create_constraint($c, $options); push @constraint_defs, $constr if($constr); - unless ( $indexed_fields{ ($c->fields())[0] } || $c->type ne FOREIGN_KEY ) { - push @index_defs, "INDEX (" . $generator->quote(($c->fields())[0]) . ")"; - $indexed_fields{ ($c->fields())[0] } = 1; + unless ( $indexed_fields{ join("\0", $c->fields) } || $c->type ne FOREIGN_KEY ) { + push @index_defs, "INDEX (" . join(', ' => map $generator->quote($_), $c->fields) . ")"; + $indexed_fields{ join("\0", $c->fields) } = 1; + $indexed_fields{ ($c->fields)[0] } = 1; } } diff --git a/t/38-mysql-producer.t b/t/38-mysql-producer.t index 446186bdf..59e775843 100644 --- a/t/38-mysql-producer.t +++ b/t/38-mysql-producer.t @@ -19,7 +19,7 @@ use FindBin qw/$Bin/; #============================================================================= BEGIN { - maybe_plan(73, + maybe_plan(76, 'YAML', 'SQL::Translator::Producer::MySQL', 'Test::Differences', @@ -800,3 +800,76 @@ EOV is(SQL::Translator::Producer::MySQL::alter_drop_constraint($constraint,$options), 'ALTER TABLE `table` DROP PRIMARY KEY','valid drop primary key'); } + +{ # do not create an extra index for a FK that is also the PK + my $sqlt; + $sqlt = SQL::Translator->new( + show_warnings => 1, + no_comments => 1, + from => "YAML", + to => "MySQL", + quote_table_names => 1, + quote_field_names => 1 + ); + + my $out = $sqlt->translate(\<<'EOT') or die "Translate error:".$sqlt->error; +--- +schema: + tables: + thing: + name: thing + extra: + order: 1 + fields: + id1: + name: id1 + data_type: int + is_primary_key: 1 + order: 1 + is_foreign_key: 0 + id2: + name: id2 + data_type: int + is_primary_key: 1 + order: 2 + is_foreign_key: 0 + constraints: + - type: PRIMARY_KEY + fields: + - id1 + - id2 + thing2: + name: thing2 + extra: + order: 2 + fields: + thing2_id1: + name: thing2_id1 + data_type: int + is_primary_key: 1 + order: 1 + is_foreign_key: 1 + thing2_id2: + name: thing2_id2 + data_type: int + is_primary_key: 1 + order: 2 + is_foreign_key: 1 + constraints: + - type: PRIMARY_KEY + fields: + - thing2_id1 + - thing2_id2 + - reference_table: thing + type: FOREIGN_KEY + fields: + - thing2_id1 + - thing2_id2 + name: fk_thing +EOT + + note $out; + like $out, qr/PRIMARY KEY \(`thing2_id1`, `thing2_id2`\)/, 'created primary key'; + unlike $out, qr/INDEX \(`thing2_id1`, `thing2_id2`\)/, 'no extra index'; + unlike $out, qr/INDEX \(`thing2_id1`\)/, 'no single column index'; +}