Search code examples
prisma

Does Prisma support composite keys with one of the attribute being NULL for PostgreSQL?


In PostgreSQL you can make a table with 2 columns as the composite key of that table, with one of them being NULL-able. I was just wondering how I can achieve this with Prisma.

In the current version of Prisma that I have (3.14.0), Prisma does allow composite key using @@id([column1,column2]), but only if those two columns are mandatory.


Solution

  • No, Prisma doesn't support composite keys with optional columns. ID Definitions are required.

    Example:

    This would work

    model Post {
      title   String
      content String
    
      @@id([title, content])
    }
    

    But this wouldn't as column content is defined as optional

    model Post {
      title   String
      content String?
    
      @@id([title, content])
    }
    

    You can create a Feature Request here to support allowing nullable columns in composite id.