Hello,
I'm trying to migrate my database schemas from prisma to the payload managed database.
I used to have a "uid" user id field that used the default value expression auth.uid() made available by Supabase.
I had this line in my schema.prisma :
uid String? @default(dbgenerated("auth.uid()")) @db.Uuidis there a way to create a field that will have the same behavior ?
For payload it might be text field that it doesn't have to change or make available in any UIs at all, but behind the hood it uses this supabase generated default value.
Any ideas on how to achieve this ?
maybe, probably, i need to use this ?
maybe something like
db: postgresAdapter({
afterSchemaInit: [
({ schema, extendTable, adapter }) => {
extendTable({
table: schema.tables.clients,
columns: {
uid: uuid.default(sql`auth.uid()`),
}
})
return schema
},
],i'll try it out
I couldn't make it work...
I tried using beforeSchemaInit and afterSchemaInit, when I managed to get it running it didnt seem to change anything on my pgAdmin.
I tried with :
beforeSchemaInit: [
({ schema, adapter }) => {
adapter.rawTables.clients = {
...adapter.rawTables.clients,
}
adapter.rawTables.clients.columns = {
...adapter.rawTables.clients.columns,
uid: {
name: 'uid',
type: 'uuid',
default: { sql: 'auth.uid()' },
}
}
adapter.rawTables.clients.indexes = {
...adapter.rawTables.clients.indexes,
uid_idx: {
name: 'uid_idx',
unique: true,
on: ['uid'],
}
} afterSchemaInit: [
({ schema, adapter, extendTable }) => {
extendTable({
table: schema.tables.clients,
columns: {
uid: {
name: 'uid',
type: 'uuid',
default: { sql: 'auth.uid()' },
}
},
extraConfig: (table) => {
uid_idx: index('uid_idx').on(table.uid)
}
})and other combinations of before/after init schema. I'm at my wit's end
⨯ TypeError: columnBuilder.build is not a function
at extendTable (app/(payload)/payload.config.js:115:8)
113 | afterSchemaInit: [
114 | ({ schema, adapter, extendTable }) => {
> 115 | extendTable({
| ^
116 | table: schema.tables.clients,
117 | columns: {
118 | uid: { {
digest: '3986122377'Hey, this should work for you:
beforeSchemaInit: [
({ schema, adapter }) => {
schema.rawTabes.clients.columns.uid = { name: 'uid', type: 'uuid', defaultRandom: true }
return schema;
}]
then you can add an index with
schema.rawTables.clients.indexes.uid_idx = {...}first, thanks for coming to the rescue !
🤔 i thought I had to use the expression made available by supabase in order for it to be tied to the auth schema and work with Supabase Auth
the auth.uid thing
This just generates a random UUID via the db, I don't know about supabase but I don't think we have some integration with it
auth.uid()is it specific to supabase?
yes, its specific to it. I don't know much about it but I know that its just a random uuid with some functionnality added to it
with prisma i used to have
uid String? @default(dbgenerated("auth.uid()")) @db.Uuidthen
rawTablesdon't expose that API, you need to use
extendTablewith: (in
afterSchemaInituid: uuid("uid").default(sql`auth.uid()`)so you can add that directly via drizzle
i did try that, thats the first thing i tried it gave an error because it didnt have uuid wait trying it again
import { uuid, index } from '@payloadcms/db-postgres/drizzle/pg-core'
db: postgresAdapter({
afterSchemaInit: [
({ schema, extendTable, adapter }) => {
extendTable({
table: schema.tables.clients
,
columns: {
uid: uuid("uid").default(sql`auth.uid()`),
},
extraConfig: (table) => ({
uid_idx: index('uid_idx').on(
table.uid,
),
}),
})
return schema
},
],
}),ohhh the import !
fuck i am bad
why dont i think when i see Error: uuid is not defined
thank you so so much
i assume i need
import { sql } from 'drizzle-orm'i'm getting this error
⨯ [error: schema "auth" does not exist]don't need from
drizzle-orm, just
import { sql } @payloadcms/db-postgres/drizzle,
okay
auth is a schema created by supabase auth btw
but you're using the public schema?
for clients
yes i am, and the whole point of that db generator is to not have to deal with the auth schema
to be fair i'm trying to migrate something i implemented long ago but i feel like i can probably solve it now
oh i forgot i'm still on a local postgres database for debugging it will probably be fixed as soon as my drizzle orm is connected to supabase
i'll probably be fine now thank you very much for your help!! 🤍
Star
Discord
online
Get dedicated engineering support directly from the Payload team.