WITH CHECK OPTION: keep writes inside a filter
Overview
A filtered updatable view can allow changes that make rows disappear from its own result. PostgreSQL CHECK OPTION rejects inserts or updates through a qualifying view when the resulting row violates the applicable view predicate. SQLite lacks native CHECK OPTION; this fixture uses a restricted update trigger with an explicit guard, not a general substitute.
First example
BEGIN;UPDATE vw_unchecked SET active=0 WHERE id=1;SELECT (SELECT COUNT(*) FROM vw_unchecked WHERE id=1),(SELECT active FROM vw_product WHERE id=1);ROLLBACK;Observed SQLite result (rows in JSON):
[ [ 0, 0 ]]How it works
The first example uses an unchecked view and makes product 1 inactive; it disappears from the view but remains in the table. The second changes only the price through vw_guarded, preserving active=1. A negative test rejects active=0 with RAISE(ABORT). The native PostgreSQL lab contrasts LOCAL with CASCADED: LOCAL checks the current view predicate and respects underlying explicit checks, while CASCADED also checks underlying automatically updatable view predicates. The guard applies to the implemented view write path, not direct base-table writes. A base-table CHECK is appropriate for universal invariants.
Practical example
BEGIN;UPDATE vw_guarded SET price=125 WHERE id=1;SELECT id,price,active FROM vw_product WHERE id=1;ROLLBACK;Observed SQLite result (rows in JSON):
[ [ 1, 125, 1 ]]Practice and mistakes
Practice: try changing active to 0 through vw_guarded, catch the expected error and inspect product 1. Hint 1: check the trigger predicate. Hint 2: RAISE(ABORT) rejects the statement; roll back explicitly if inside a transaction. Expect active to remain 1. Mistake: assuming a view predicate alone protects writes or direct table access.