DT
Drizzle Teaminterdrifter

How to properly do foreign keys in schema with PlanetScale?

I'm aware that foreign key constrains aren't supported, but foreign keys themselves are. push:
sqlMessage: 'VT10001: foreign key constraints are not allowed'
sqlMessage: 'VT10001: foreign key constraints are not allowed'
export const session = mysqlTable("auth_session", {
id: varchar("id", { length: 128 }).primaryKey(),
userId: varchar("user_id", { length: 15 }).notNull().references(() => user.id),
// ...
});
export const session = mysqlTable("auth_session", {
id: varchar("id", { length: 128 }).primaryKey(),
userId: varchar("user_id", { length: 15 }).notNull().references(() => user.id),
// ...
});
shaw93
shaw93ā€¢303d ago
Hey Murad around 4mins50sec this subject is discussed: https://www.youtube.com/watch?v=d7XJjQesDtE&ab_channel=DaveGray
Dave Gray
YouTube
Build a Random Quote Machine with Nextjs, PlanetScale MySQL, & Driz...
Web Dev Roadmap for Beginners (Free!): https://bit.ly/DaveGrayWebDevRoadmap Build a random quote machine with Nextjs, PlanetScale MySQL, & Drizzle ORM. In this tutorial, we'll set up a MySQL database on PlanetScale, connect to and query the database with Drizzle ORM, and build a Next.js frontend and API routes. šŸš© Subscribe āžœ https://bit.ly/3n...
ejoo
ejooā€¢289d ago
https://planetscale.com/docs/learn/operating-without-foreign-key-constraints
import { pgTable, serial, text, integer } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';

export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name'),
});

export const usersRelations = relations(users, ({ many }) => ({
posts: many(posts),
}));

export const posts = pgTable('posts', {
id: serial('id').primaryKey(),
content: text('content'),
authorId: integer('author_id'),
});

export const postsRelations = relations(posts, ({ one }) => ({
author: one(users, {
fields: [posts.authorId],
references: [users.id],
}),
}));
import { pgTable, serial, text, integer } from 'drizzle-orm/pg-core';
import { relations } from 'drizzle-orm';

export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: text('name'),
});

export const usersRelations = relations(users, ({ many }) => ({
posts: many(posts),
}));

export const posts = pgTable('posts', {
id: serial('id').primaryKey(),
content: text('content'),
authorId: integer('author_id'),
});

export const postsRelations = relations(posts, ({ one }) => ({
author: one(users, {
fields: [posts.authorId],
references: [users.id],
}),
}));
Operating without foreign key constraints ā€” PlanetScale Documentati...
How to manage your relational data without formal foreign key constraints in your schema