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?
Was this field created and then you added unique afterwards?
I don't quite remember tbh, but I can test that pretty easily.
You may need to add this index manually. Can you check in the DB if there is a unique constraint?
indeed there isn't one for email, only for id
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.
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 🙂
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
Discord
online
Get dedicated engineering support directly from the Payload team.