go: fkconstrain: check: Correctly handle types.Null values in tuples being checked.

This commit is contained in:
Aaron Son
2021-06-11 12:07:26 -07:00
parent 77e87a517b
commit 7243848381
2 changed files with 30 additions and 2 deletions
+2 -2
View File
@@ -133,7 +133,7 @@ func (declFKC declaredFKCheck) Check(ctx context.Context, _, newTV row.TaggedVal
declTag := declFKC.refTagsToDeclTags[refTag]
keyTupVals[i*2] = types.Uint(refTag)
if val, ok := newTV[declTag]; ok {
if val, ok := newTV[declTag]; ok && !types.IsNull(val) {
keyTupVals[i*2+1] = val
} else {
// full key is not present. skip check
@@ -184,7 +184,7 @@ func (refFKC referencedFKCheck) Check(ctx context.Context, oldTV, _ row.TaggedVa
for i, tag := range indexColTags {
keyTupVals[i*2] = types.Uint(tag)
if val, ok := oldTV[tag]; ok {
if val, ok := oldTV[tag]; ok && !types.IsNull(val) {
keyTupVals[i*2+1] = val
} else {
// full key is not present. skip check
+28
View File
@@ -1802,3 +1802,31 @@ SQL
[[ "$output" =~ '1452' ]] || false # first ensure the proper code
[[ "$output" =~ 'cannot add or update a child row - Foreign key violation on fk: `color_fk`, table: `objects`, referenced table: `colors`, key: `(4011,"yellow")`' ]] || false
}
@test "foreign-keys: updating to null works as expected in commit" {
dolt sql -q "create table unprocessed_t (id int primary key);"
dolt sql -q "create table additional_t (id int primary key);"
dolt sql <<SQL
create table t (
id int primary key,
unprocessed_id int,
foreign key (unprocessed_id) references unprocessed_t(id) on delete cascade on update cascade,
additional_id int,
foreign key (additional_id) references additional_t(id) on delete cascade on update cascade
);
SQL
dolt add .
dolt commit -m 'schema'
dolt sql -q 'insert into additional_t values (20)'
dolt sql -q 'insert into t (id, additional_id) values (1,20);'
dolt add .
dolt commit -m 'initial'
dolt sql -q 'insert into unprocessed_t values (20)'
dolt sql -q 'update t set additional_id = null, unprocessed_id = 20 where id = 1'
dolt sql -q 'delete from additional_t'
dolt add .
dolt commit -m 'this should not break'
}