Skip to content

Instantly share code, notes, and snippets.

@calvinmetcalf
Forked from scf4/knexDeferAllForeignKeys.js
Last active January 30, 2020 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save calvinmetcalf/1aae3ca1eca6d54a3c5a1c74471a7874 to your computer and use it in GitHub Desktop.
Save calvinmetcalf/1aae3ca1eca6d54a3c5a1c74471a7874 to your computer and use it in GitHub Desktop.
export default async (knex) => {
const foreignKeys = await knex('pg_constraint')
.select('pg_constraint.conname', 'pg_class.relname')
.where('contype', '=', 'f')
.join('pg_class').on('pg_class.oid', 'pg_constraint.conrelid');
return Promise.all(foreignKeys.map(({relname, conname}) =>
knex.schema.raw(`
ALTER TABLE ??
ALTER CONSTRAINT ??
DEFERRABLE INITIALLY DEFERRED;
`, [relname, conname])
));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment