Skip to content

Implement CASCADE behavior enforcement for FOREIGN KEY constraints #2336

@mathiasrw

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions