Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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