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.

Option "unique" doesn't work

default discord avatar
mathewcstlast year
7

Hey there, I'm trying to build a simple collection to get emails from the main website (using the REST API)



Here's my collection:



import { CollectionConfig } from 'payload/types'
import { loggedIn } from './access/loggedIn'

export const Newsletter: CollectionConfig = {
  slug: 'newsletter',
  labels: {
    singular: 'Newsletter',
    plural: 'Newsletters',
  },
  admin: {
    useAsTitle: 'name',
    defaultColumns: ['name', 'email', 'updatedAt'],
  },
  access: {
    read: loggedIn,
    create: () => true,
    update: loggedIn,
    delete: loggedIn,
  },
  fields: [
    {
      name: 'name',
      label: 'Nome',
      type: 'text',
      index: true,
    },
    {
      name: 'email',
      label: 'Email',
      type: 'email',
      index: true,
      unique: true,
    },
  ],
}


Although email has a

unique: true

, it allows me to make multiple requests with the same email and all get saved in the database (as the image attached shows)



Any ideas?

  • discord user avatar
    denolfe
    last year

    Was this field created and then you added unique afterwards?

  • default discord avatar
    mathewcstlast year

    I don't quite remember tbh, but I can test that pretty easily.

  • discord user avatar
    denolfe
    last year

    You may need to add this index manually. Can you check in the DB if there is a unique constraint?

  • default discord avatar
    mathewcstlast year

    indeed there isn't one for email, only for id

  • discord user avatar
    denolfe
    last year

    I think it's possible to get in a bad state if unique is added after the fact. I'd say manually add it for now.



    We'll probably tighten up this DX in the future.

  • default discord avatar
    mathewcstlast year

    oh I can just create a new field (i.e.: user_email). I didn't know about the "unique as the field is created". Thanks a lot 🙂

  • default discord avatar
    marco.avilalast year

    I'm using Postgres and it still doesn't work even if I run

    migrate fresh

    which drops the entire database. Any way on how to make it work with postgres database?

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.