I have a text-field
uid
which initially had
unique
as
false
. I was able to create multiple documents with the same
uid
.
I wanted to set the field as
unique: true
, but apparently it still allows me to have duplicates across.
This happens even after I delete all the documents and start adding them from scratch. The behaviour of the id being
not unique
is retained.
Is there a proper way to convert these fields ?
The same is in reverse as well, i.e converting a field from
unique: true
to
unique: false
. Payload prevents adding duplicates for the field across.
You likely already have an index on the field and so Payload will not replace it on start up.
All you need to do is drop any existing indexes that are out of date. Then start Payload again and you should see the index get created. You might also have to correct your bad collection data.
This experience will be improved in 2 along with the database changes. We're introducing migrations.
@dribbens I am also trying to convert a non-unique field to unique. I have dropped the index in mongo collection, restarted payload but the index is not created again. The only way payload creates a new index is if the field's name changes. Ie. from slug to slugy. But if I change it back to slug it still wont create an index.
Is this expected behaviour?
Not expected at all, I can't say I've seen this before.
If you don't have any data you can drop the collection. That will make the index on start for sure. If you have data then you have to make the index manually.
I will have to try see if I can reproduce. What version of mongodb are you running?
I'm working on building proper migrations into Payload core for 2.0 right now for what it's worth.
Yeah, I dropped the collection, then the index got created again.
Mongo version is 6.0.4
Dropping the collection really shouldn't have been necessary. Since there wasn't already an index in place, I'm surprised it wasn't re-created properly on start.
Could be my lack of understanding though
Hi Dan. Can confirm the issue is happening. Changing a field from and to unique does not update the MongoDB field. Using Payload 1.9.1, MongoDB Atlas
Star
Discord
online
Get help straight from the Payload team with an Enterprise License.