Skip to main content

Documentation Index

Fetch the complete documentation index at: https://planetscale.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

What this error means

NO_UNIQUE_KEY means a table does not have a usable unique, not-null key. A PRIMARY KEY satisfies this requirement. A UNIQUE KEY can also satisfy it if every indexed column is NOT NULL and the key does not use a text/blob column or a prefix index. A common error message is:
table "orders" has no non-null unique key: all tables must have at least one unique, not-null key without using text / blob columns or partial indexes.

Why PlanetScale rejects it

PlanetScale uses Vitess online schema change workflows to apply changes without downtime. Those workflows need a stable row identity while copying and reconciling data. Nullable unique keys do not qualify because MySQL allows multiple NULL values in a unique index.

How to fix it

Add a primary key or a unique key over columns that are all declared NOT NULL.
CREATE TABLE orders (
  id bigint unsigned NOT NULL,
  customer_id bigint unsigned NOT NULL,
  created_at datetime NOT NULL,
  PRIMARY KEY (id)
);
If the table already exists and the intended key column contains NULL values, fix the data first, then make the column NOT NULL, then add the unique key. For large tables, split those steps into separate deploy requests.
ALTER TABLE orders MODIFY id bigint unsigned NOT NULL;
ALTER TABLE orders ADD UNIQUE KEY orders_id_idx (id);
If your only candidate key uses a TEXT, BLOB, or prefix index, use a surrogate key such as a bigint or UUID column instead.

Need help?

Get help from the PlanetScale Support team, or join our Discord community to see how others are using PlanetScale.