createNoFkUpdateTrigger method

  1. @override
List<String> createNoFkUpdateTrigger(
  1. QualifiedTablename table,
  2. List<String> pk
)
override

Create a trigger that prevents updates to the primary key.

Implementation

@override
List<String> createNoFkUpdateTrigger(
  QualifiedTablename table,
  List<String> pk,
) {
  final namespace = table.namespace;
  final tablename = table.tablename;
  return [
    '''
CREATE TRIGGER update_ensure_${namespace}_${tablename}_primarykey
BEFORE UPDATE ON $table
BEGIN
SELECT
  CASE
    ${pk.map(
          (col) => '''
WHEN old."$col" != new."$col" THEN
    \t\tRAISE (ABORT, 'cannot change the value of column $col as it belongs to the primary key')''',
        ).join('\n')}
  END;
END;''',
  ];
}