melhawnuhra
melhawnuhra
DTDrizzle Team
Created by melhawnuhra on 10/14/2024 in #help
Need help migrating from "reference" declarations to "foreignKeys"
I'm doing some refactoring on our codebase (to eliminate circular dependencies) One of the main culprits is the <name>.table.ts files we have defined for our schema. I want to avoid generating unnecessary migrations, so was trying to use the same names that were automatically assigned for the references. After making the logical changes and generating a migration, I end up with a whole bunch of lines dropping the existing foreign keys, but none adding. Like this:
ALTER TABLE "transaction" DROP CONSTRAINT "transaction_wallet_id_wallet_id_fk";
ALTER TABLE "transaction" DROP CONSTRAINT "transaction_source_wallet_id_wallet_id_fk";
ALTER TABLE "trial_subscription" DROP CONSTRAINT "trial_subscription_entity_id_entity_id_fk";
ALTER TABLE "transaction" DROP CONSTRAINT "transaction_wallet_id_wallet_id_fk";
ALTER TABLE "transaction" DROP CONSTRAINT "transaction_source_wallet_id_wallet_id_fk";
ALTER TABLE "trial_subscription" DROP CONSTRAINT "trial_subscription_entity_id_entity_id_fk";
An example of one change: In trialSubscription.table.ts:
entityId:
uuid('entity_id').notNull(),
- .references(() => entity.id)
.notNull()
entityId:
uuid('entity_id').notNull(),
- .references(() => entity.id)
.notNull()
In foreignKeys.ts:
+ export const foreignKeys = {
+ trialSubscriptionEntityFk: foreignKey({
+ name: 'trial_subscription_entity_id_entity_id_fk',
+ columns: [trialSubscription.entityId],
+ foreignColumns: [entity.id],
+ }),
+ }
+ export const foreignKeys = {
+ trialSubscriptionEntityFk: foreignKey({
+ name: 'trial_subscription_entity_id_entity_id_fk',
+ columns: [trialSubscription.entityId],
+ foreignColumns: [entity.id],
+ }),
+ }
In db.ts:
+ import { foreignKeys } from './db/foreignKeys'

export const schema = {
<existing schema>
+ ...foreignKeys
+ import { foreignKeys } from './db/foreignKeys'

export const schema = {
<existing schema>
+ ...foreignKeys
How can I accomplish what we need to do here? Assistance greatly appreciated!
2 replies