-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add foreign key (entry_id, tenant_id) for favorites (#79)
- Loading branch information
1 parent
0e12659
commit c6764a7
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/db/migrations/20240224171740_favorites_add_entry_id_tenant_id_foreign_key.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
`); | ||
} |