Skip to content

Implement CASCADE behavior enforcement for FOREIGN KEY constraints #2336

Open

Description

@mathiasrw

AlaSQL currently parses CASCADE syntax but does not enforce the behavior. When a parent row is deleted or updated, child rows with ON DELETE CASCADE or ON UPDATE CASCADE are not automatically affected.

What needs to be implemented

ON DELETE actions:

  • CASCADE: When a row in the parent table is deleted, automatically delete all rows in child tables that reference it
  • SET NULL: Set foreign key columns to NULL in child tables when parent is deleted
  • SET DEFAULT: Set foreign key columns to their default value when parent is deleted
  • RESTRICT: Prevent deletion of parent row if child rows exist
  • NO ACTION: Same as RESTRICT per SQL-99 standard

ON UPDATE actions:

  • CASCADE: When parent key is updated, update matching foreign keys in child tables
  • SET NULL: Set foreign key to NULL when parent key is updated
  • SET DEFAULT: Set foreign key to default when parent key is updated
  • RESTRICT: Prevent update of parent key if child rows exist
  • NO ACTION: Same as RESTRICT per SQL-99 standard

Implementation areas

  • Modify DELETE statement handler (src/74delete.js) to check for child tables and apply CASCADE actions
  • Modify UPDATE statement handler (src/74update.js) to check for child tables and apply CASCADE actions
  • Store foreign key metadata including ondelete/onupdate actions in table definition during CREATE TABLE
  • Add validation for RESTRICT actions before allowing delete/update
  • Handle circular dependencies and prevent infinite loops

Related

Activity

  1. mathiasrw commented on Oct 21, 2026

    @mathiasrw
    MemberAuthor

    The NIST SQL Test Suite expects the SQL-92 default for a foreign key without ON DELETE or ON UPDATE, which is NO ACTION: deleting or changing a parent key that a child row still uses fails, and nothing changes. 27 checks in 14 files depend on it (cdr008.sql ... cdr011.sql, cdr022.sql, ...). Reproduced on develop (38bf3c3):

    alasql('CREATE TABLE p (id CHAR(2) PRIMARY KEY)');
    alasql("INSERT INTO p VALUES ('E1'), ('E2')");
    alasql('CREATE TABLE c (pid CHAR(2) REFERENCES p(id))');
    alasql("INSERT INTO c VALUES ('E1')");
    alasql("DELETE FROM p WHERE id = 'E1'");   // 1, the child row now points at nothing
    alasql("UPDATE c SET pid = 'E9'");         // Foreign key "E9" not found in table "p" (the child side is checked)

    Expected: the DELETE fails with an integrity constraint violation (SQLSTATE 23000) and p keeps E1.

  2. modified the milestones: v5.x Focus, v5.0.0 on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions