Prevent ALTER TABLE from being used to add a column with a REFERENCES clause and a non-NULL default value while foreign key support is enabled.
FossilOrigin-Name: 353b1b18253ab71ba38a887e555994f5469b87bd
This commit is contained in:
@@ -58,6 +58,8 @@ ifcapable {!foreignkey||!trigger} {
|
||||
# fkey2-13.*: Test that FK processing is performed when a row is REPLACED by
|
||||
# an UPDATE or INSERT statement.
|
||||
#
|
||||
# fkey2-14.*: Test the ALTER TABLE and DROP TABLE commands.
|
||||
#
|
||||
# fkey2-genfkey.*: Tests that were used with the shell tool .genfkey
|
||||
# command. Recycled to test the built-in implementation.
|
||||
#
|
||||
@@ -737,6 +739,44 @@ do_test fkey2-13.1.4 {
|
||||
}
|
||||
} {2 2 2 3 2 3 1}
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
# The following tests, fkey2-14.*, test that the "DROP TABLE" and "ALTER
|
||||
# TABLE" commands work as expected wrt foreign key constraints.
|
||||
#
|
||||
# fkey2-14.1*: ALTER TABLE ADD COLUMN
|
||||
# fkey2-14.2*: ALTER TABLE RENAME TABLE
|
||||
# fkey2-14.3*: DROP TABLE
|
||||
#
|
||||
drop_all_tables
|
||||
do_test fkey2-14.1 {
|
||||
# Adding a column with a REFERENCES clause is not supported.
|
||||
execsql {
|
||||
CREATE TABLE t1(a PRIMARY KEY);
|
||||
CREATE TABLE t2(a, b);
|
||||
}
|
||||
catchsql { ALTER TABLE t2 ADD COLUMN c REFERENCES t1 }
|
||||
} {0 {}}
|
||||
do_test fkey2-14.2 {
|
||||
catchsql { ALTER TABLE t2 ADD COLUMN d DEFAULT NULL REFERENCES t1 }
|
||||
} {0 {}}
|
||||
do_test fkey2-14.3 {
|
||||
catchsql { ALTER TABLE t2 ADD COLUMN e REFERENCES t1 DEFAULT NULL}
|
||||
} {0 {}}
|
||||
do_test fkey2-14.4 {
|
||||
catchsql { ALTER TABLE t2 ADD COLUMN f REFERENCES t1 DEFAULT 'text'}
|
||||
} {1 {Cannot add a REFERENCES column with non-NULL default value}}
|
||||
do_test fkey2-14.5 {
|
||||
catchsql { ALTER TABLE t2 ADD COLUMN g DEFAULT CURRENT_TIME REFERENCES t1 }
|
||||
} {1 {Cannot add a REFERENCES column with non-NULL default value}}
|
||||
do_test fkey2-14.5 {
|
||||
execsql {
|
||||
PRAGMA foreign_keys = off;
|
||||
ALTER TABLE t2 ADD COLUMN h DEFAULT 'text' REFERENCES t1;
|
||||
PRAGMA foreign_keys = on;
|
||||
SELECT sql FROM sqlite_master WHERE name='t2';
|
||||
}
|
||||
} {{CREATE TABLE t2(a, b, c REFERENCES t1, d DEFAULT NULL REFERENCES t1, e REFERENCES t1 DEFAULT NULL, h DEFAULT 'text' REFERENCES t1)}}
|
||||
|
||||
#-------------------------------------------------------------------------
|
||||
# The following block of tests, those prefixed with "fkey2-genfkey.", are
|
||||
# the same tests that were used to test the ".genfkey" command provided
|
||||
|
||||
Reference in New Issue
Block a user