Liam
Liam
Explore posts from servers
DTDrizzle Team
Created by Liam on 7/31/2024 in #help
There is not enough information to infer relation
Hello, I am running into a issue where drizzle studio is stating that there is not enought information to infer relation "__public__.tickets.chat". Any idea why this might be? I have the following schema:
export const tickets = pgTable("tickets", {
id: text("id").primaryKey(),
title: varchar("title", { length: 255 }).notNull(),
description: text("description").notNull(),
status: ticketStatus("status").notNull().default("awaiting"),
createdAt: timestamp("created_at").notNull().defaultNow(),
});

export const ticketRelations = relations(tickets, ({ one, many }) => ({
chat: one(chats),
tickets: many(ticketsToUsers),
}));

export const chats = pgTable("chats", {
id: text("id").primaryKey(),
type: chatType("type").notNull(),
ticketID: text("ticket_id").references(() => tickets.id),
author: text("author").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
});

export const chatRelations = relations(chats, ({ many }) => ({
messages: many(chatMessages),
members: many(chatsToUsers),
}));
export const tickets = pgTable("tickets", {
id: text("id").primaryKey(),
title: varchar("title", { length: 255 }).notNull(),
description: text("description").notNull(),
status: ticketStatus("status").notNull().default("awaiting"),
createdAt: timestamp("created_at").notNull().defaultNow(),
});

export const ticketRelations = relations(tickets, ({ one, many }) => ({
chat: one(chats),
tickets: many(ticketsToUsers),
}));

export const chats = pgTable("chats", {
id: text("id").primaryKey(),
type: chatType("type").notNull(),
ticketID: text("ticket_id").references(() => tickets.id),
author: text("author").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
});

export const chatRelations = relations(chats, ({ many }) => ({
messages: many(chatMessages),
members: many(chatsToUsers),
}));
5 replies
DTDrizzle Team
Created by Liam on 7/28/2024 in #help
Drizzle Query Returns Different Result Than Select
Made a issue over on github for this, and wanted to see if anyone has ran into anything similar. Thanks! https://github.com/drizzle-team/drizzle-orm/issues/2703
1 replies
DTDrizzle Team
Created by Liam on 5/25/2024 in #help
Error When Using isNull Filter
No description
1 replies
DTDrizzle Team
Created by Liam on 5/22/2024 in #help
Drizzle-Zod Set Default Minimum Length For Text / No Empty Strings
Hello! I am using drizzle zod to validate some input, at the moment the schema for any text columns allows empty strings. I would like to require that all strings cannnot be empty. Is there any way to do this without having to manually define each column as z.string().min(1)? Thanks!
1 replies
DTDrizzle Team
Created by Liam on 5/4/2024 in #help
Issue With Defining References
Hello! I am attempting to define a reference to another table. I have the following users table:
export const users = pgTable("users", {
userID: serial("user_id").primaryKey(),
clerkID: text("clerk_id").unique(),
firstName: text("first_name").notNull(),
lastName: text("last_name").notNull(),
email: text("email").notNull().unique(),
role: text("role").notNull().default("member"),
});

export const usersRelations = relations(users, ({ one, many }) => ({
data: one(data, { fields: [users.userID], references: [data.userID] }),
}));

export const data = pgTable("data", {
userID: integer("user_id").primaryKey(),
// some other data in here
});
export const users = pgTable("users", {
userID: serial("user_id").primaryKey(),
clerkID: text("clerk_id").unique(),
firstName: text("first_name").notNull(),
lastName: text("last_name").notNull(),
email: text("email").notNull().unique(),
role: text("role").notNull().default("member"),
});

export const usersRelations = relations(users, ({ one, many }) => ({
data: one(data, { fields: [users.userID], references: [data.userID] }),
}));

export const data = pgTable("data", {
userID: integer("user_id").primaryKey(),
// some other data in here
});
however when update the tables to have the references like so
export const users = pgTable("users", {
userID: serial("user_id").primaryKey().references(() => data.userID),
clerkID: text("clerk_id").unique(),
firstName: text("first_name").notNull(),
lastName: text("last_name").notNull(),
email: text("email").notNull().unique(),
role: text("role").notNull().default("member"),
});

export const usersRelations = relations(users, ({ one, many }) => ({
data: one(data, { fields: [users.userID], references: [data.userID] }),
}));

export const data = pgTable("data", {
userID: integer("user_id").primaryKey().references(() => users.userID),
// some other data here
});
export const users = pgTable("users", {
userID: serial("user_id").primaryKey().references(() => data.userID),
clerkID: text("clerk_id").unique(),
firstName: text("first_name").notNull(),
lastName: text("last_name").notNull(),
email: text("email").notNull().unique(),
role: text("role").notNull().default("member"),
});

export const usersRelations = relations(users, ({ one, many }) => ({
data: one(data, { fields: [users.userID], references: [data.userID] }),
}));

export const data = pgTable("data", {
userID: integer("user_id").primaryKey().references(() => users.userID),
// some other data here
});
it gives me a type error on both tables:
'data' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.ts(
'data' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.ts(
Any idea why this might be? Thanks for the help!
1 replies
DTDrizzle Team
Created by Liam on 3/6/2024 in #help
Using Drizzle w/ Postgrest
Hi! I was wondering if there is a way to use drizzle w/ postgrest so I can use drizzle on edge. I could not find anything about this in the docs so I thought I'd ask here. Thanks!
1 replies
DTDrizzle Team
Created by Liam on 2/5/2024 in #help
Invalid Input Value for Enum
Hello! I am using a enum but running into a weird issue when trying to set the value as one of options in enum. The following is the sections from my schema: Error: Error: invalid input value for enum status: "expired" Drizzle Schema Table:
export const discordVerification = pgTable("discord_verification", {
code: varchar("code", { length: 255 }).notNull().primaryKey(),
createdAt: timestamp("created_at").notNull().defaultNow(),
clerkID: varchar("clerk_id", { length: 255 }),
discordUserID: varchar("discord_user_id", { length: 255 }),
discordUserTag: varchar("discord_user_tag", { length: 255 }),
discordProfilePhoto: varchar("discord_profile_photo", { length: 255 }),
discordName: varchar("discord_name", { length: 255 }),
status: discordVerificationStatus("status").notNull().default("pending"),
});
export const discordVerification = pgTable("discord_verification", {
code: varchar("code", { length: 255 }).notNull().primaryKey(),
createdAt: timestamp("created_at").notNull().defaultNow(),
clerkID: varchar("clerk_id", { length: 255 }),
discordUserID: varchar("discord_user_id", { length: 255 }),
discordUserTag: varchar("discord_user_tag", { length: 255 }),
discordProfilePhoto: varchar("discord_profile_photo", { length: 255 }),
discordName: varchar("discord_name", { length: 255 }),
status: discordVerificationStatus("status").notNull().default("pending"),
});
Drizzle Schema Enum:
export const discordVerificationStatus = pgEnum("status", [
"pending",
"expired",
"accepted",
"rejected",
]);
export const discordVerificationStatus = pgEnum("status", [
"pending",
"expired",
"accepted",
"rejected",
]);
Any idea why this might be? Thanks!
5 replies
DTDrizzle Team
Created by Liam on 12/27/2023 in #help
Drizzle query with where clause and primary composite key
Hello, I have defined a composite key and am now trying to run a drizzle query to lookup the event where that key exists. I have defined the following query, is this the fastest way to go about doing this lookup? Thanks! Drizzle Schema Table:
export const scans = pgTable(
"scans",
{
createdAt: timestamp("created_at").notNull().defaultNow(),
userID: varchar("user_id", { length: 255 }).notNull(),
eventID: integer("event_id").notNull(),
count: integer("count").notNull(),
},
(table) => ({
id: primaryKey({ columns: [table.userID, table.eventID] }),
})
);
export const scans = pgTable(
"scans",
{
createdAt: timestamp("created_at").notNull().defaultNow(),
userID: varchar("user_id", { length: 255 }).notNull(),
eventID: integer("event_id").notNull(),
count: integer("count").notNull(),
},
(table) => ({
id: primaryKey({ columns: [table.userID, table.eventID] }),
})
);
Lookup:
const scan = await db.query.scans.findFirst({
where: and(eq(scans.eventID, eventID), eq(scans.userID, userID)),
});
const scan = await db.query.scans.findFirst({
where: and(eq(scans.eventID, eventID), eq(scans.userID, userID)),
});
Thanks for the help!
2 replies
DTDrizzle Team
Created by Liam on 7/29/2023 in #help
Cannot Access Primary Key on Table
Hello, I am trying to access the primary key named id on the following table:
export const invites = mysqlTable(
"invites",
{
inviteeID: varchar("invitee_id", { length: 255 }).notNull(),
teamID: varchar("team_id", { length: 50 }).notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
status: mysqlEnum("status", ["pending", "accepted", "declined"]).notNull().default("pending"),
},
(table) => ({
id: primaryKey(table.inviteeID, table.teamID),
})
);
export const invites = mysqlTable(
"invites",
{
inviteeID: varchar("invitee_id", { length: 255 }).notNull(),
teamID: varchar("team_id", { length: 50 }).notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
status: mysqlEnum("status", ["pending", "accepted", "declined"]).notNull().default("pending"),
},
(table) => ({
id: primaryKey(table.inviteeID, table.teamID),
})
);
I am using the following command to try to update a row but I am getting an error and the id field does not show on the invite. Any idea why this might be? Thanks!
await db.update(invites).set({ status: "accepted" }).where(eq(invites.id, user.invites[0].id));
await db.update(invites).set({ status: "accepted" }).where(eq(invites.id, user.invites[0].id));
4 replies
DTDrizzle Team
Created by Liam on 7/19/2023 in #help
Error When Applying Migrations Related to Text Indexes
Hello, I am attempting to apply some new migrations, however I am getting the following error when attempting to do so. Any idea why this is? Thanks! Error: DatabaseError: target: hackkit.-.primary: vttablet: BLOB/TEXT column 'presigned_url' used in key specification without a key length (errno 1170) (sqlstate 42000) (CallerID: v6f912c7oc1zkrupmyrv): Sql: "create table files (\n\tpresigned_url text not null,\n\tkey varchar(500) not null,\n\tvalidated boolean not null default false,\n\ttype enum('generic', 'resume') not null,\n\towner_id varchar(255) not null,\n\tconstraint files_presigned_url_unique UNIQUE key (presigned_url),\n\tconstraint files_key_unique UNIQUE key (key)\n)", BindVars: {REDACTED} Section of schema.ts
export const files = mysqlTable("files", {
id: varchar("id", { length: 255 }).notNull().primaryKey().unique(),
presignedURL: text("presigned_url"),
key: varchar("key", { length: 500 }).notNull().unique(),
validated: boolean("validated").notNull().default(false),
type: mysqlEnum("type", ["generic", "resume"]).notNull(),
ownerID: varchar("owner_id", { length: 255 }).notNull(),
});

export const filesRelations = relations(files, ({ one }) => ({
owner: one(users, {
fields: [files.ownerID],
references: [users.clerkID],
}),
}));
export const files = mysqlTable("files", {
id: varchar("id", { length: 255 }).notNull().primaryKey().unique(),
presignedURL: text("presigned_url"),
key: varchar("key", { length: 500 }).notNull().unique(),
validated: boolean("validated").notNull().default(false),
type: mysqlEnum("type", ["generic", "resume"]).notNull(),
ownerID: varchar("owner_id", { length: 255 }).notNull(),
});

export const filesRelations = relations(files, ({ one }) => ({
owner: one(users, {
fields: [files.ownerID],
references: [users.clerkID],
}),
}));
1 replies
DTDrizzle Team
Created by Liam on 7/18/2023 in #help
Importing drizzle-zod Schemas on the Client
Hi, Is it ok to import schemas generated with drizzle-zod via createInsertSchema on the client? I have my schema in a separate schema.ts file away from where I init my database if that matters at all. Thanks!
16 replies
DTDrizzle Team
Created by Liam on 7/8/2023 in #help
Drizzle-zod Include Relations
Hello, I was wondering if drizzle-zod includes support for relations, and if so how I can include them. Thanks!
10 replies
DTDrizzle Team
Created by Liam on 7/5/2023 in #help
Drizzle Count # of Records
What would be the best way to count records in drizzle? Thanks!
7 replies
DTDrizzle Team
Created by Liam on 7/4/2023 in #help
Inserting with Relation
Hi, What is the way of inserting a new record with a relationship in Drizzle. I understand that Prisma abstracts this away as if you are inserting everything as once but under the hood it breaks it up into multiple queries. Is there anything like this in drizzle?
4 replies
DTDrizzle Team
Created by Liam on 7/2/2023 in #help
How to apply migrations on postgres in a serverless enviroment?
Hello, I want to apply postgres migrations to my vercel DB. I see that the push command does not work for postgres and am therefore attempting to use the migrate function like so:
// ./src/db/index.ts
import { sql } from "@vercel/postgres";
import { drizzle } from "drizzle-orm/vercel-postgres";
import { migrate } from "drizzle-orm/vercel-postgres/migrator";
import * as schema from "./schema";

export const db = drizzle(sql, { schema });

await migrate(db, { migrationsFolder: "../../drizzle" });
// ./src/db/index.ts
import { sql } from "@vercel/postgres";
import { drizzle } from "drizzle-orm/vercel-postgres";
import { migrate } from "drizzle-orm/vercel-postgres/migrator";
import * as schema from "./schema";

export const db = drizzle(sql, { schema });

await migrate(db, { migrationsFolder: "../../drizzle" });
However this does not seem like the best way to do this as that would make it so migrations are applied only one I am attempting to make a call instead of at something like build time as far as I can tell. Additionally, it is additional imports that will have to be loaded in the serverless and/or edge environment. Is there a better way to do this?
6 replies
DTDrizzle Team
Created by Liam on 6/16/2023 in #help
Using PgArray
Hello, How can I use postgres arrays in drizzle? I want to have one column to be a string array. Thanks!
2 replies
DTDrizzle Team
Created by Liam on 6/13/2023 in #help
What does this section of the docs mean?
Under the docs for indexes and constraints it says that drizzle kit only supports index name and on() param. What does this mean? Would the following not work with drizzle kit then?
export const users = mysqlTable("users", {
clerkID: varchar("clerk_id", {length: 255}).notNull().primaryKey(),
email: varchar("email", {length: 255}).notNull(),
publicUsername: varchar("public_username", {length: 50}).notNull(),
}, (table) => {
return {
publicUsernameIdx: uniqueIndex("public_username_idx").on(table.publicUsername)
}
});
export const users = mysqlTable("users", {
clerkID: varchar("clerk_id", {length: 255}).notNull().primaryKey(),
email: varchar("email", {length: 255}).notNull(),
publicUsername: varchar("public_username", {length: 50}).notNull(),
}, (table) => {
return {
publicUsernameIdx: uniqueIndex("public_username_idx").on(table.publicUsername)
}
});
3 replies
DTDrizzle Team
Created by Liam on 6/12/2023 in #help
Getting Started With Drizzle And Had A Few Questions
Hi! I'm getting started with drizzle after having been a long time Prisma user. I had a few questions about Drizzle as I'm getting started: 1. Is there a "cuid()" type of functionality where instead of using a integer key I can use a CUID? 2. How can I set non-id fields to be unique (In prisma, this was doable with the @unique)? Thanks in advance for the help!
1 replies