From b6caf02a70098a3b0e68b0c60a80b096c58fa13a Mon Sep 17 00:00:00 2001 From: Erik Grinaker Date: Thu, 18 Jul 2024 14:03:56 +0200 Subject: [PATCH] sql: add (failing) test case for referenced primary key updates --- src/sql/testscripts/writes/update_primary_key | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/sql/testscripts/writes/update_primary_key b/src/sql/testscripts/writes/update_primary_key index 7ec6df332..cc12f3fc9 100644 --- a/src/sql/testscripts/writes/update_primary_key +++ b/src/sql/testscripts/writes/update_primary_key @@ -147,3 +147,27 @@ Error: invalid input: invalid primary key NULL !> UPDATE "int" SET id = id + 1 --- Error: invalid input: primary key 1 already exists + +# UPDATE can modify multiple primary keys that have foreign key references +# as long as the final set of primary keys still satisfy the references. +# We test this by swapping primary keys 0 <-> 1 in a single UPDATE. +# TODO: this shouldn't error. +> CREATE TABLE ref (id INT PRIMARY KEY, int_id INTEGER REFERENCES "int") +> INSERT INTO ref VALUES (0, 0), (1, 1) +--- +ok + +!> UPDATE "int" SET id = 1 - id +> SELECT * FROM "int" +--- +Error: invalid input: row referenced by ref.int_id for ref.id=0 +0 +1 + +# If the UPDATE leaves a foreign key dangling, it should error. +!> UPDATE "int" SET id = id - 1 +> SELECT * FROM "int" +--- +Error: invalid input: row referenced by ref.int_id for ref.id=0 +0 +1