Plugin storage uniqueIndexes are enforced across every plugin and collection (non-partial unique index on _plugin_storage)
What happens
A plugin's uniqueIndexes on one storage collection enforce uniqueness on every collection of every plugin that stores the same JSON field.
generateCreateIndexSql (src/plugins/storage-indexes.ts) builds a unique index like this:
CREATE UNIQUE INDEX "uidx_plugin_<plugin>_<collection>_<field>"
ON _plugin_storage(plugin_id, collection, json_extract(data, '$.<field>'))The comment above it says the leading (plugin_id, collection) columns "scope it per plugin/collection — including unique-index semantics". For lookups that holds. For uniqueness it doesn't: the constraint applies to the (plugin_id, collection, value) tuple of every row in _plugin_storage. So plugin A's uniqueIndexes: ["sku"] on variants also forbids two rows with the same sku in plugin B's movements. Plugin B never declared a unique index, and the error names plugin A's index.
The index also outlives the plugin that declared it. Removing plugin A from astro.config leaves uidx_plugin_a_* in the database, still enforcing.
Repro (plain SQLite, same DDL)
CREATE TABLE _plugin_storage (plugin_id TEXT, collection TEXT, id TEXT, data TEXT,
PRIMARY KEY (plugin_id, collection, id));
-- what createStorageIndexes creates for plugin "shop", collection "variants", uniqueIndexes: ["sku"]
CREATE UNIQUE INDEX "uidx_plugin_shop_variants_sku"
ON _plugin_storage(plugin_id, collection, json_extract(data, '$.sku'));
-- another plugin, another collection, no unique index declared
INSERT INTO _plugin_storage VALUES ('stock', 'movements', 'm1', '{"sku":"ABC","qty":5}');
INSERT INTO _plugin_storage VALUES ('stock', 'movements', 'm2', '{"sku":"ABC","qty":-1}');
-- Error: UNIQUE constraint failed: index 'uidx_plugin_shop_variants_sku'Where we hit it
The site was on EmDash 1.0.1 (Astro, SQLite locally, D1 in production), and had first run the DashCommerce plugin. DashCommerce declares unique indexes on product_variants.sku, coupons.code, customers.email, orders.orderNumber and others.
We then replaced it with otta.sh, which writes one row per inventory movement and one per reservation, several per sku. The second add-to-cart of any sku failed with:
UNIQUE constraint failed: index 'uidx_plugin_dashcommerce_product_variants_sku'That was after DashCommerce had been removed from the config. We worked around it by dropping the uidx_plugin_dashcommerce_* indexes and their _plugin_indexes rows by hand.
Any two plugins that both store common field names (sku, code, email, slug, orderNumber) can hit this as soon as one of them declares the field unique.
Suggested fix
Make unique indexes partial:
CREATE UNIQUE INDEX … ON _plugin_storage(plugin_id, collection, json_extract(data, '$.sku'))
WHERE plugin_id = 'shop' AND collection = 'variants'The planner concern in the comment (SQLite and D1 won't choose a partial index under bound parameters without ANALYZE) matters for lookups, not for enforcement. SQLite checks a partial unique index on every write whatever the query planner does. With the partial form, the repro above accepts both stock rows and still rejects a real duplicate in shop/variants.
If a unique field also needs a fast lookup, it can keep the current non-partial non-unique index next to it.
Separately, it might be worth dropping, or at least reporting, a plugin's uidx_* indexes once that plugin is no longer registered. Today the only trace is _plugin_indexes.
Existing databases would need a migration that recreates the current uidx_plugin_* indexes as partial ones.
Generated with Claude Code
Source: emdash-cms/emdash