Trappola
Soft delete e NULL
WHERE NOT is_deleted scarta anche le righe in cui il flag è NULL.NOT NULL è NULL, e WHERE tiene solo le righe vere. Quando un flag di cancellazione può essere vuoto (una colonna
aggiunta dopo, un vecchio sistema), scrivi WHERE is_deleted IS NOT TRUE, o meglio trasformalo una volta per tutte in un
vero booleano nello staging: COALESCE(is_deleted, false) AS is_deleted.
La trappola
WITH products(sku, is_deleted) AS (VALUES ('A', false), ('B', true), ('C', NULL))
SELECT sku FROM products WHERE NOT is_deleted ORDER BY sku;
| sku |
|---|
| A |
La correzione
WITH products(sku, is_deleted) AS (VALUES ('A', false), ('B', true), ('C', NULL))
SELECT sku FROM products WHERE is_deleted IS NOT TRUE ORDER BY sku;
| sku |
|---|
| A |
| C |
Esercitati:Soft-deleted, or just NULL?