Skip to content

Commit 680b7ab

Browse files
committed
Add test for foreign key constraint validation in Appender
1 parent 0c928d9 commit 680b7ab

File tree

1 file changed

+32
-0
lines changed
  • crates/duckdb/src/appender

1 file changed

+32
-0
lines changed

crates/duckdb/src/appender/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,36 @@ mod test {
304304
}
305305
Ok(())
306306
}
307+
308+
#[test]
309+
fn test_appender_foreign_key_constraint() -> Result<()> {
310+
let conn = Connection::open_in_memory()?;
311+
conn.execute_batch(
312+
r"
313+
CREATE TABLE parent (id INTEGER PRIMARY KEY);
314+
CREATE TABLE child (
315+
id INTEGER,
316+
parent_id INTEGER,
317+
FOREIGN KEY (parent_id) REFERENCES parent(id)
318+
);",
319+
)?;
320+
conn.execute("INSERT INTO parent VALUES (1)", [])?;
321+
322+
let mut appender = conn.appender("child")?;
323+
appender.append_row(params![1, 999])?; // Invalid parent_id
324+
325+
// Foreign key constraint should be checked during flush
326+
match appender.flush() {
327+
Err(Error::DuckDBFailure(_, Some(msg))) => {
328+
assert_eq!(
329+
msg,
330+
"Violates foreign key constraint because key \"id: 999\" does not exist in the referenced table"
331+
);
332+
}
333+
Err(e) => panic!("Expected foreign key constraint error, got: {e:?}"),
334+
Ok(_) => panic!("Expected foreign key constraint error, but flush succeeded"),
335+
}
336+
337+
Ok(())
338+
}
307339
}

0 commit comments

Comments
 (0)