Drizzle / PlanetScale Table Relations

Does anyone possibly have a link to a repo I can take a look at to see how relations are setup with Drizzle ORM and PlanetScale as the database?
7 Replies
Sturlen
Sturlen16mo ago
the examples in the docs work with Planetscale: https://orm.drizzle.team/docs/rqb#declaring-relations
dollahane23033
dollahane23033OP16mo ago
Yes I've been through those, I also checked out this article. https://planetscale.com/blog/working-with-related-data-using-drizzle-and-planetscale
Emulating foreign key constraints with Drizzle relationships | Plan...
Learn how to build virtual relationships between tables in PlanetScale while using the Drizzle TypeScript ORM.
dollahane23033
dollahane23033OP16mo ago
For testing purposes I copied the schema from this article and still got the fk constraint error. The tables were created to the db though, I havnt tried actually writting to it yet. Maybe it actually works. Just wanted to see how others have done theirs. I also found this somewhere in the docs "export const db = drizzle(connection, { schema, mode: 'planetscale' });"
Sturlen
Sturlen16mo ago
here's the config that I use for Planetscale and relations
// drizzle.config.ts
import type { Config } from "drizzle-kit";

export default {
schema: "./src/server/schema.ts",
out: "./drizzle",
driver: "mysql2",
dbCredentials: {
connectionString,
},
} satisfies Config;
// drizzle.config.ts
import type { Config } from "drizzle-kit";

export default {
schema: "./src/server/schema.ts",
out: "./drizzle",
driver: "mysql2",
dbCredentials: {
connectionString,
},
} satisfies Config;
here's the table I use for users in a project, where users can have many pets. the important is that you can't specify any foreign keys directly in the table, only through seperate relations
export const users = mysqlTable(
"users",
{
id: varchar("id", { length: 24 }).primaryKey(), //cuid2
username: varchar("username", { length: 256 }).unique().notNull(),
password_hash: varchar("password_hash", { length: 256 }).notNull(),
created_at: timestamp("created_at").defaultNow().notNull(),
},
(users) => ({
nameIndex: uniqueIndex("name_idx").on(users.username),
})
);

export const usersRelations = relations(users, ({ many }) => ({
pets: many(pets),
}));
export const users = mysqlTable(
"users",
{
id: varchar("id", { length: 24 }).primaryKey(), //cuid2
username: varchar("username", { length: 256 }).unique().notNull(),
password_hash: varchar("password_hash", { length: 256 }).notNull(),
created_at: timestamp("created_at").defaultNow().notNull(),
},
(users) => ({
nameIndex: uniqueIndex("name_idx").on(users.username),
})
);

export const usersRelations = relations(users, ({ many }) => ({
pets: many(pets),
}));
Rhys
Rhys16mo ago
https://github.com/AnswerOverflow/AnswerOverflow/blob/main/packages/db/src/schema.ts I just moved @Answer Overflow over to drizzle yesterday, here’s our schema file
GitHub
AnswerOverflow/packages/db/src/schema.ts at main · AnswerOverflow/A...
Indexing Discord Help Channel Questions into Google - AnswerOverflow/AnswerOverflow
dollahane23033
dollahane23033OP16mo ago
Thanks guys! 🙏
Want results from more Discord servers?
Add your server