Browse Source

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.
mark_story 11 years ago
parent
commit
4fe22665c0

+ 1 - 1
src/Database/Schema/PostgresSchema.php

@@ -428,7 +428,7 @@ class PostgresSchema extends BaseSchema {
 		);
 		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]),

+ 5 - 5
tests/TestCase/Database/Schema/PostgresSchemaTest.php

@@ -680,31 +680,31 @@ SQL;
 				'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'
 			],
 		];
 	}