Amruth Pillai
Amruth Pillai
Explore posts from servers
PPrisma
Created by Amruth Pillai on 6/27/2024 in #help-and-questions
Accessing extended operations on the prisma client with typescript
No description
4 replies
PPrisma
Created by Amruth Pillai on 6/27/2024 in #help-and-questions
Accessing extended operations on the prisma client with typescript
No description
4 replies
DTDrizzle Team
Created by joshborseth on 9/14/2023 in #help
TypeError: Cannot read properties of undefined (reading 'compositePrimaryKeys')
I'm getting this error when running drizzle-kit push on the latest version (0.21.0) as well, with the postgresql dialect. The schema has a lot of tables with composite primary keys, but most of them look like this:
export const workOrderTemplateUserTable = pgTable(
"work_order_template_user",
{
templateId: text("template_id")
.notNull()
.references(() => workOrderTemplateTable.id, { onDelete: "cascade" }),
userId: text("user_id")
.notNull()
.references(() => userTable.id, { onDelete: "cascade" }),
createdAt: timestampType("created_at"),
createdById: text("created_by_id").references(() => userTable.id, { onDelete: "set null" }),
},
(t) => ({
primaryKey: primaryKey({ columns: [t.templateId, t.userId] }),
}),
);
export const workOrderTemplateUserTable = pgTable(
"work_order_template_user",
{
templateId: text("template_id")
.notNull()
.references(() => workOrderTemplateTable.id, { onDelete: "cascade" }),
userId: text("user_id")
.notNull()
.references(() => userTable.id, { onDelete: "cascade" }),
createdAt: timestampType("created_at"),
createdById: text("created_by_id").references(() => userTable.id, { onDelete: "set null" }),
},
(t) => ({
primaryKey: primaryKey({ columns: [t.templateId, t.userId] }),
}),
);
This used to work as expected in the older versions, but it's become a problem now.
44 replies
DTDrizzle Team
Created by turtles#7560074 on 3/10/2024 in #help
created_at/updated_at
I use this:
createdAt: timestamp('created_at', { mode: "date", withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { mode: "date", withTimezone: true }).notNull().defaultNow(),
createdAt: timestamp('created_at', { mode: "date", withTimezone: true }).notNull().defaultNow(),
updatedAt: timestamp('updated_at', { mode: "date", withTimezone: true }).notNull().defaultNow(),
Both values would be initialised to the current date. To set up updatedAt, the recommended method is to just set updatedAt: new Date() when you are updating a record.
3 replies
DTDrizzle Team
Created by _Pear on 3/3/2024 in #help
Is it possible to store an array of primary keys for table A on table B using Postgres?
It sounds like what you are trying to create is a bridge/junction table? But I'm quite not sure. In any case, yes, you cannot have multiple primary keys in a single table, but you can have composite primary keys (PKs that constitute of two or more columns). In Drizzle for example, this is how you would do that:
export const taskUserTable = pgTable(
"task_user",
{
taskId: text("task_id")
.notNull()
.references(() => taskTable.id, { onDelete: "cascade" }),
userId: text("user_id")
.notNull()
.references(() => userTable.id, { onDelete: "cascade" }),
createdAt: timestampType("created_at"),
createdById: text("created_by_id").references(() => userTable.id, { onDelete: "set null" }),
},
(t) => ({
primaryKey: primaryKey({ columns: [t.taskId, t.userId] }),
}),
);
export const taskUserTable = pgTable(
"task_user",
{
taskId: text("task_id")
.notNull()
.references(() => taskTable.id, { onDelete: "cascade" }),
userId: text("user_id")
.notNull()
.references(() => userTable.id, { onDelete: "cascade" }),
createdAt: timestampType("created_at"),
createdById: text("created_by_id").references(() => userTable.id, { onDelete: "set null" }),
},
(t) => ({
primaryKey: primaryKey({ columns: [t.taskId, t.userId] }),
}),
);
Here, taskId and userId are both references to PKs on a different table, but used in union as a primary key to ensure there are never two rows which have the same task and user ID. You can add more columns to this primary key simply by appending the array.
7 replies