Looks like a fix is being worked on in the above mentioned GH issue (Adding UID with attached field to existing Collection Type returns null · Issue #11665 · strapi/strapi · GitHub) but for now I’ve created a bulk update script to generate slugs for every entry in my collection:
I found this service: strapi/uid.js at main · strapi/strapi · GitHub
In a custom service:
async updateCreditCardsSlug() {
const items = await strapi.entityService.findMany(
"api::item.item",
{
fields: ["id", "name"],
}
);
const pendingUpdateItems = items.map(async (item) => {
const slug = await this.generateUIDField({
contentTypeUID: "api::item.item",
field: "slug",
data: item,
});
return strapi.entityService.update(
"api::item.item",
item.id,
{
data: {
slug,
},
}
);
});
const updatedItemsResults = await Promise.allSettled(
pendingUpdateItems
);
const updatedItems = updatedItemsResults.filter(
(result) => result.status === "fulfilled"
);
const failedItems = updatedItemsResults.filter(
(result) => result.status === "rejected"
);
return {
updatedItems: updatedItems.length,
failedItems: failedItems.length,
};
},
I couldn’t figure out how to import or get access to that uid service so I copied the code from that service and included those functions on my custom service. Would be better to import it.