Merge 'allow null for unique columns and don't validate fkeys on parent' from Pavan Nambi

closes #3966
closes #3965
check constraint pr depends on this.

Reviewed-by: Preston Thorpe <preston@turso.tech>

Closes #3969
This commit is contained in:
Preston Thorpe
2025-11-16 19:59:59 -05:00
committed by GitHub
4 changed files with 39 additions and 3 deletions

View File

@@ -968,8 +968,6 @@ impl Schema {
None
} else {
find_parent_unique(&parent_cols)
.ok_or_else(|| fk_mismatch_err(&child.name, &parent_tbl.name))?
.into()
};
fk.validate()?;
out.push(ResolvedFkRef {

View File

@@ -1648,7 +1648,7 @@ fn translate_column(
} else if let Some(default_expr) = column.default.as_ref() {
translate_expr(program, None, default_expr, column_register, resolver)?;
} else {
let nullable = !column.notnull() && !column.primary_key() && !column.unique();
let nullable = !column.notnull() && !column.primary_key();
if !nullable {
crate::bail_parse_error!(
"column {} is not nullable",

View File

@@ -1207,3 +1207,34 @@ do_execsql_test_in_memory_any_error fk-update-parent-multi-col-pk-2 {
INSERT INTO child VALUES(1, 2);
UPDATE parent SET b = 3 WHERE a = 1 AND b = 2;
}
# https://github.com/tursodatabase/turso/issues/3965
do_execsql_test_on_specific_db {:memory:} fk-no-val-on-parent {
PRAGMA foreign_keys=ON;
CREATE TABLE t1(
a INT,
b INT
);
CREATE TABLE t2(
x INT,
y INT,
FOREIGN KEY (x, y) REFERENCES t1(a, b)
);
INSERT INTO t1(a, b) VALUES (5, 10);
SELECT * FROM t1;
} {5|10}
do_execsql_test_in_memory_any_error fk-val-on-parent {
PRAGMA foreign_keys=ON;
CREATE TABLE t1(
a INT,
b INT
);
CREATE TABLE t2(
x INT,
y INT,
FOREIGN KEY (x, y) REFERENCES t1(a, b)
);
INSERT INTO t2(a, b) VALUES (5, 10);
SELECT * FROM t2;
}

View File

@@ -26,3 +26,10 @@ do_execsql_test sel-true {
do_execsql_test sel-false {
select false;
} {0}
# https://github.com/tursodatabase/turso/issues/3966
do_execsql_test_on_specific_db {:memory:} not-null-just-cuz-unique {
create table t (a int, x int unique);
insert into t(a) values(1);
select * from t;
} {1|}