Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Use supabase auth.uid() expression as default value in a uuid field

default discord avatar
teneburulast year
12

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.Uuid

is 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 ?


https://payloadcms.com/docs/database/postgres#afterschemainit

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'
  • default discord avatar
    ritsu0455last year

    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 = {...}
  • default discord avatar
    teneburulast year

    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

  • default discord avatar
    ritsu0455last year

    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?

  • default discord avatar
    teneburulast year

    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.Uuid
  • default discord avatar
    ritsu0455last year

    then

    rawTables

    don't expose that API, you need to use

    extendTable

    with: (in

    afterSchemaInit
    uid:  uuid("uid").default(sql`auth.uid()`)


    so you can add that directly via drizzle

  • default discord avatar
    teneburulast year

    i did try that, thats the first thing i tried it gave an error because it didnt have uuid wait trying it again

  • default discord avatar
    ritsu0455last year
    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
          },
        ],
      }),
  • default discord avatar
    teneburulast year

    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]
  • default discord avatar
    ritsu0455last year

    don't need from

    drizzle-orm

    , just

    import { sql } @payloadcms/db-postgres/drizzle

    ,

  • default discord avatar
    teneburulast year

    okay



    auth is a schema created by supabase auth btw

  • default discord avatar
    ritsu0455last year

    but you're using the public schema?



    for clients

  • default discord avatar
    teneburulast year

    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 on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.