如何使用 drizzle-orm 在 PlanetScale(MYSQL) 中定义关系?

问题描述 投票:0回答:1

我正在尝试学习 drizzle-orm,但问题是我正在使用 Planetscale 并且它很粗糙。 我想要做的是为用户授予管理员或编辑者角色。 此外,具有管理员角色的用户将拥有一个或多个与其关联的编辑器,并且, 具有编辑者角色的用户将有一个与其关联的管理员用户。 我绝不是一个 SQL 专家,事实上我几乎不了解 SQL(我已经开始研究 SQL 了:))。 事实上,Planetscale 不支持外键,这使得它变得更难(对我来说)。

这是我遇到的错误,我尝试运行 drizzle studio 来查看我的数据库。

我应该如何定义所需的关系?

➜  galzzy git:(main) ✗ pnpm drizzle-kit studio
No config path provided, using default path
Reading config file '/home/izaan/Work/galzzy/drizzle.config.ts'
/home/izaan/Work/galzzy/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/drizzle-orm/index-1899b9ae.cjs:4180
    throw new Error(`There is not enough information to infer relation "${sourceTableTsName}.${relation.fieldName}"`);
          ^

Error: There is not enough information to infer relation "users.editors"
    at normalizeRelation (/home/izaan/Work/galzzy/node_modules/.pnpm/[email protected]_@[email protected][email protected]/node_modules/drizzle-orm/index-1899b9ae.cjs:4180:11)
    at /home/izaan/Work/galzzy/node_modules/.pnpm/@[email protected]/node_modules/@drizzle-team/studio/index.cjs:51168:67
    at Array.map (<anonymous>)
    at /home/izaan/Work/galzzy/node_modules/.pnpm/@[email protected]/node_modules/@drizzle-team/studio/index.cjs:51167:42
    at Array.map (<anonymous>)
    at extractRelations (/home/izaan/Work/galzzy/node_modules/.pnpm/@[email protected]/node_modules/@drizzle-team/studio/index.cjs:51166:51)
    at prepareServer (/home/izaan/Work/galzzy/node_modules/.pnpm/@[email protected]/node_modules/@drizzle-team/studio/index.cjs:51198:21)
    at async Command.<anonymous> (/home/izaan/Work/galzzy/node_modules/.pnpm/[email protected]/node_modules/drizzle-kit/index.cjs:53825:18)

Node.js v18.18.0

这是我的架构 我再次希望具有管理员角色的用户将拥有一个或多个与其关联的编辑器,并且, 具有编辑者角色的用户将有一个与其关联的管理员用户。

我是否正确地进行了架构设计?

import { int, timestamp, mysqlTable, primaryKey, varchar, text, } from 'drizzle-orm/mysql-core'; import type { AdapterAccount } from '@auth/core/adapters'; import { relations } from 'drizzle-orm'; export const users = mysqlTable('user', { id: varchar('id', { length: 255 }).notNull().primaryKey(), name: varchar('name', { length: 255 }), email: varchar('email', { length: 255 }).notNull(), emailVerified: timestamp('emailVerified', { mode: 'date', fsp: 3, }).defaultNow(), image: varchar('image', { length: 255 }), role: varchar<'role', string, ['ADMIN' | 'EDITOR']>('role', { length: 6, }).notNull(), adminId: varchar('adminId', { length: 255 }), }); export const accounts = mysqlTable( 'account', { userId: varchar('userId', { length: 255 }).notNull(), type: varchar('type', { length: 255 }) .$type<AdapterAccount['type']>() .notNull(), provider: varchar('provider', { length: 255 }).notNull(), providerAccountId: varchar('providerAccountId', { length: 255 }).notNull(), refresh_token: varchar('refresh_token', { length: 255 }), refresh_token_expires_in: int('refresh_token_expires_in'), access_token: varchar('access_token', { length: 255 }), expires_at: int('expires_at'), token_type: varchar('token_type', { length: 255 }), scope: varchar('scope', { length: 255 }), id_token: varchar('id_token', { length: 2048 }), session_state: varchar('session_state', { length: 255 }), }, (account) => ({ compoundKey: primaryKey(account.provider, account.providerAccountId), }) ); export const sessions = mysqlTable('session', { sessionToken: varchar('sessionToken', { length: 255 }).notNull().primaryKey(), userId: varchar('userId', { length: 255 }).notNull(), expires: timestamp('expires', { mode: 'date' }).notNull(), }); export const verificationTokens = mysqlTable( 'verificationToken', { identifier: varchar('identifier', { length: 255 }).notNull(), token: varchar('token', { length: 255 }).notNull(), expires: timestamp('expires', { mode: 'date' }).notNull(), }, (vt) => ({ compoundKey: primaryKey(vt.identifier, vt.token), }) ); export const userRelations = relations(users, ({ many, one }) => ({ accounts: many(accounts), sessions: many(sessions), editors: many(users, { relationName: 'editors' }), adminId: one(users, { fields: [users.adminId], references: [users.id], }), })); export const accountsRelations = relations(accounts, ({ one }) => ({ user: one(users, { fields: [accounts.userId], references: [users.id], }), })); export const sessionRelations = relations(sessions, ({ one }) => ({ user: one(users, { fields: [sessions.userId], references: [users.id], }), }));
    
sql typescript prisma planetscale drizzle
1个回答
0
投票
Planetscale 确实支持外键约束。这可能会有所帮助

https://planetscale.com/blog/working-with-lated-data-using-drizzle-and-planetscale

© www.soinside.com 2019 - 2024. All rights reserved.