Skip to content

Commit

Permalink
Add foreign key (entry_id, tenant_id) for favorites (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
imsitnikov authored Feb 26, 2024
1 parent 0e12659 commit c6764a7
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {Knex} from 'knex';

export async function up(knex: Knex): Promise<void> {
return knex.raw(`
ALTER TABLE favorites DROP CONSTRAINT favorites_entries_id;
ALTER TABLE favorites DROP CONSTRAINT favorites_tenant_id_ref;
ALTER TABLE entries
ADD CONSTRAINT entries_uniq_entry_id_tenant_id_constraint UNIQUE(entry_id, tenant_id);
ALTER TABLE favorites
ADD CONSTRAINT favorites_entry_id_tenant_id_ref FOREIGN KEY (entry_id, tenant_id)
REFERENCES entries(entry_id, tenant_id) ON DELETE CASCADE ON UPDATE CASCADE;
`);
}

export async function down(knex: Knex): Promise<void> {
return knex.raw(`
ALTER TABLE favorites DROP CONSTRAINT favorites_entry_id_tenant_id_ref;
ALTER TABLE entries DROP CONSTRAINT entries_uniq_entry_id_tenant_id_constraint;
ALTER TABLE favorites
ADD CONSTRAINT favorites_tenant_id_ref FOREIGN KEY (tenant_id)
REFERENCES tenants(tenant_id) ON UPDATE CASCADE ON DELETE CASCADE;
ALTER TABLE favorites
ADD CONSTRAINT favorites_entries_id FOREIGN KEY (entry_id)
REFERENCES entries(entry_id)
ON DELETE CASCADE;
`);
}

0 comments on commit c6764a7

Please sign in to comment.