Skip to content

Instantly share code, notes, and snippets.

@scf4
Created June 18, 2017 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scf4/03780af508218200a590959d8258f61c to your computer and use it in GitHub Desktop.
Save scf4/03780af508218200a590959d8258f61c to your computer and use it in GitHub Desktop.
export default async (knex) => {
const foreignKeys = await knex('pg_constraint')
.select('*')
.where('contype', '=', 'f');
const queries = foreignKeys.map(async foreignKey => {
const [keyData] = await knex('pg_class')
.select('relname')
.where('oid', '=', foreignKey.conrelid);
const tableName = keyData.relname;
return knex.schema.raw(`
ALTER TABLE "${tableName}"
ALTER CONSTRAINT ${foreignKey.conname}
DEFERRABLE INITIALLY DEFERRED;
`);
});
await Promise.all(queries);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment