bennettcohen
bennettcohen
DTDrizzle Team
Created by bennettcohen on 12/18/2024 in #help
Column not created even though appears in migration file
Hey team - I'm noticing a column that isn't created in my database, even though the schema + migration file clearly show that it is. Setup: - docker postgres running locally - drizzle-kit: "^0.28.0" - drizzle-orm: "^0.36.1" I've confirmed it's in the migration file.
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "licenses" (
OTHER COLS
"status" text DEFAULT 'not_verified'
);
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "licenses" (
OTHER COLS
"status" text DEFAULT 'not_verified'
);
But then it does not appear in the database. Any tips for diagnosing this?
2 replies
DTDrizzle Team
Created by bennettcohen on 11/20/2024 in #help
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.
1 replies
DTDrizzle Team
Created by bennettcohen on 11/18/2024 in #help
Shared db across services
Hey folks - we have two services that need access to the same database (a client facing API and our web app). I don't really want to setup a monorepo. I think my best bet is to either setup a shared git submodule or a private npm package? What's the best approach here for creating this + running migrations? Rn I'm literally copying and pasting code across the projects and want something a little more put together and in sync. Thanks!
2 replies
DTDrizzle Team
Created by bennettcohen on 7/17/2024 in #help
Connecting with SSL
No description
2 replies
DTDrizzle Team
Created by bennettcohen on 7/11/2024 in #help
Type safety on .set() method?
Hey folks - I was wondering if there is a way to get type safety when updating a row using the .set method? In the example below, updated_at is the table name, not the property name updatedAt. This code runs with no errors, but of course never updates the updated at because it doesn't exist!
const data = await db
.update(verifications)
.set({
status: 'inactive',
updated_at: sql`NOW()`,
})
.where(and(eq(sql`${persons.id}::text`, personId)))
.returning();
const data = await db
.update(verifications)
.set({
status: 'inactive',
updated_at: sql`NOW()`,
})
.where(and(eq(sql`${persons.id}::text`, personId)))
.returning();
Any tips for getting this to be type-safe ?
12 replies