Skip to content
Permalink
Browse files

Make postgres foreign keys deferrable.

Having deferable keys will be important for fixtures. If a constraint
is not deferable, it cannot be turned off temporarily. Since the current
schema tools are generally for creating fixture schema we'll assume
people want that feature as it is pretty handy.
  • Loading branch information...
markstory committed Sep 4, 2014
1 parent f7c2b59 commit 4fe22665c04add92c8d889ba0a36359efd1ee9c8
Showing with 6 additions and 6 deletions.
  1. +1 −1 src/Database/Schema/PostgresSchema.php
  2. +5 −5 tests/TestCase/Database/Schema/PostgresSchemaTest.php
@@ -428,7 +428,7 @@ protected function _keySql($prefix, $data) {
);
if ($data['type'] === Table::CONSTRAINT_FOREIGN) {
return $prefix . sprintf(
' FOREIGN KEY (%s) REFERENCES %s (%s) ON UPDATE %s ON DELETE %s',
' FOREIGN KEY (%s) REFERENCES %s (%s) ON UPDATE %s ON DELETE %s DEFERABLE INITIALLY IMMEDIATE',
implode(', ', $columns),
$this->_driver->quoteIdentifier($data['references'][0]),
$this->_driver->quoteIdentifier($data['references'][1]),
@@ -680,31 +680,31 @@ public static function constraintSqlProvider() {
'author_id_idx',
['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id']],
'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
'REFERENCES "authors" ("id") ON UPDATE RESTRICT ON DELETE RESTRICT'
'REFERENCES "authors" ("id") ON UPDATE RESTRICT ON DELETE RESTRICT DEFERABLE INITIALLY IMMEDIATE'
],
[
'author_id_idx',
['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'cascade'],
'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
'REFERENCES "authors" ("id") ON UPDATE CASCADE ON DELETE RESTRICT'
'REFERENCES "authors" ("id") ON UPDATE CASCADE ON DELETE RESTRICT DEFERABLE INITIALLY IMMEDIATE'
],
[
'author_id_idx',
['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'restrict'],
'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
'REFERENCES "authors" ("id") ON UPDATE RESTRICT ON DELETE RESTRICT'
'REFERENCES "authors" ("id") ON UPDATE RESTRICT ON DELETE RESTRICT DEFERABLE INITIALLY IMMEDIATE'
],
[
'author_id_idx',
['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'setNull'],
'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
'REFERENCES "authors" ("id") ON UPDATE SET NULL ON DELETE RESTRICT'
'REFERENCES "authors" ("id") ON UPDATE SET NULL ON DELETE RESTRICT DEFERABLE INITIALLY IMMEDIATE'
],
[
'author_id_idx',
['type' => 'foreign', 'columns' => ['author_id'], 'references' => ['authors', 'id'], 'update' => 'noAction'],
'CONSTRAINT "author_id_idx" FOREIGN KEY ("author_id") ' .
'REFERENCES "authors" ("id") ON UPDATE NO ACTION ON DELETE RESTRICT'
'REFERENCES "authors" ("id") ON UPDATE NO ACTION ON DELETE RESTRICT DEFERABLE INITIALLY IMMEDIATE'
],
];
}

0 comments on commit 4fe2266

Please sign in to comment.
You can’t perform that action at this time.