Hi,
I’m trying to create unique indexes with Strapi and MySQL.
The reason is that if I receive two requests very close to each other, it’s possible that a field which is flagged as unique: true
in the schema.json
won’t be enforced to be unique, and duplicated entries can occur.
I’ve tried to write a migration script:
await knex.schema.alterTable(uniqueIndex.table, (table) => {
table.unique([uniqueIndex.field], {
indexName: `unique_${uniqueIndex.field}`,
useConstraint: false,
})
})
However, Strapi instantly removes the index after the migration run. (I’ve tried to constantly refresh the mysql, and there was a split second while the index was there and then it has been removed).
Another solution was to extend the schema.json
:
{
"kind": "collectionType",
"collectionName": "...",
"info": {
"singularName": "...",
"pluralName": "...",
"displayName": "...",
"description": ""
},
"options": {
"draftAndPublish": false
},
"pluginOptions": {
"content-manager": {
"editable": false
}
},
"attributes": {
...
},
"indexes": [
{
"name": "unique_price_id",
"columns": ["price_id"],
"type": "unique"
}
]
}
The problem with this approach is that each time I modify the schema with the Content-Type Builder, the indexes
will be removed from the JSON.
Is there a way to create unique indexes which won’t be removed?
Thank you,
David
This topic has been created from a Discord post (1236606493583867996) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord