Migration Order Question

Hey team - I'm trying to understand migration order as I keep running into the annoying error
error: column "created_by_api_key_id" referenced in foreign key constraint does not exist
error: column "created_by_api_key_id" referenced in foreign key constraint does not exist
I separate my schema into different files below. Can someone explain how to fix this? Occurs for all my definitions. Thanks!
// api-keys.ts ....
import { organizationId } from "./organizations";

export const apiKeys = pgTable("api_keys", {
id,
name: text("name").notNull(),
preview: text("preview").notNull(),
hashedKey: text("hashed_key").notNull(),
organizationId,
...timestamps,
}, (table) => {
return {
hashedKeyIdx: uniqueIndex("hashed_key_idx").on(table.hashedKey),
};
});

// I export a column reference so I can have one place if i decide to change stuff
export const createdByApiKeyId = uuid("created_by_api_key_id")
.references(() => apiKeys.id);
// api-keys.ts ....
import { organizationId } from "./organizations";

export const apiKeys = pgTable("api_keys", {
id,
name: text("name").notNull(),
preview: text("preview").notNull(),
hashedKey: text("hashed_key").notNull(),
organizationId,
...timestamps,
}, (table) => {
return {
hashedKeyIdx: uniqueIndex("hashed_key_idx").on(table.hashedKey),
};
});

// I export a column reference so I can have one place if i decide to change stuff
export const createdByApiKeyId = uuid("created_by_api_key_id")
.references(() => apiKeys.id);
// applications.ts ...
import { createdByApiKeyId } from "./api-keys";
import { organizationId } from "./organizations";

export const applications = pgTable("applications", {
id,
name: text("name").notNull(),
organizationId,
createdByApiKeyId, // MARK: I reference the column definition
...timestamps,
});

export const applicationId = uuid("application_id")
.references(() => applications.id);
// applications.ts ...
import { createdByApiKeyId } from "./api-keys";
import { organizationId } from "./organizations";

export const applications = pgTable("applications", {
id,
name: text("name").notNull(),
organizationId,
createdByApiKeyId, // MARK: I reference the column definition
...timestamps,
});

export const applicationId = uuid("application_id")
.references(() => applications.id);
Interestingly enough, it seems like it's fine with organizationId, which has a similar setup.
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server