Skip to content

Commit

Permalink
Merge branch 'issue-8739' into master.
Browse files Browse the repository at this point in the history
Refs #8739
  • Loading branch information
markstory committed May 7, 2016
2 parents 5081daf + afd03b6 commit bd24bdb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Database/Schema/Table.php
Expand Up @@ -603,10 +603,12 @@ public function addConstraint($name, $attrs)
$attrs['columns']
));

$this->_constraints[$name]['references'][1] = array_unique(array_merge(
(array)$this->_constraints[$name]['references'][1],
[$attrs['references'][1]]
));
if (isset($this->_constraints[$name]['references'])) {
$this->_constraints[$name]['references'][1] = array_unique(array_merge(
(array)$this->_constraints[$name]['references'][1],
[$attrs['references'][1]]
));
}
return $this;
}
} else {
Expand Down
33 changes: 33 additions & 0 deletions tests/TestCase/Database/Schema/TableTest.php
Expand Up @@ -303,6 +303,39 @@ public function testAddConstraint()
$this->assertEquals(['primary'], $table->constraints());
}

/**
* Test adding an constraint with an overlapping unique index
* >
* @return void
*/
public function testAddConstraintOverwriteUniqueIndex()
{
$table = new Table('articles');
$table->addColumn('project_id', [
'type' => 'integer',
'default' => null,
'limit' => 11,
'null' => false,
])->addColumn('id', [
'type' => 'integer',
'autoIncrement' => true,
'limit' => 11
])->addColumn('user_id', [
'type' => 'integer',
'default' => null,
'limit' => 11,
'null' => false,
])->addConstraint('users_idx', [
'type' => 'unique',
'columns' => ['project_id', 'user_id']
])->addConstraint('users_idx', [
'type' => 'foreign',
'references' => ['users', 'project_id', 'id'],
'columns' => ['project_id', 'user_id']
]);
$this->assertEquals(['users_idx'], $table->constraints());
}

/**
* Dataprovider for invalid addConstraint calls.
*
Expand Down

0 comments on commit bd24bdb

Please sign in to comment.