Skip to content

Add test for foreign key constraint validation in Appender #565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions crates/duckdb/src/appender/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl fmt::Debug for Appender<'_> {

#[cfg(test)]
mod test {
use crate::{Connection, Result};
use crate::{params, Connection, Error, Result};

#[test]
fn test_append_one_row() -> Result<()> {
Expand Down Expand Up @@ -264,7 +264,6 @@ mod test {
#[test]
#[cfg(feature = "chrono")]
fn test_append_datetime() -> Result<()> {
use crate::params;
use chrono::{NaiveDate, NaiveDateTime};

let db = Connection::open_in_memory()?;
Expand All @@ -285,8 +284,7 @@ mod test {
}

#[test]
fn test_appender_error() -> Result<(), crate::Error> {
use crate::params;
fn test_appender_error() -> Result<()> {
let conn = Connection::open_in_memory()?;
conn.execute(
r"CREATE TABLE foo (
Expand All @@ -298,12 +296,44 @@ mod test {
)?;
let mut appender = conn.appender("foo")?;
match appender.append_row(params!["foo"]) {
Err(crate::Error::DuckDBFailure(.., Some(msg))) => {
Err(Error::DuckDBFailure(.., Some(msg))) => {
assert_eq!(msg, "Call to EndRow before all columns have been appended to!")
}
Err(err) => panic!("unexpected error: {err:?}"),
Ok(_) => panic!("expected an error but got Ok"),
}
Ok(())
}

#[test]
fn test_appender_foreign_key_constraint() -> Result<()> {
let conn = Connection::open_in_memory()?;
conn.execute_batch(
r"
CREATE TABLE parent (id INTEGER PRIMARY KEY);
CREATE TABLE child (
id INTEGER,
parent_id INTEGER,
FOREIGN KEY (parent_id) REFERENCES parent(id)
);",
)?;
conn.execute("INSERT INTO parent VALUES (1)", [])?;

let mut appender = conn.appender("child")?;
appender.append_row(params![1, 999])?; // Invalid parent_id

// Foreign key constraint should be checked during flush
match appender.flush() {
Err(Error::DuckDBFailure(_, Some(msg))) => {
assert_eq!(
msg,
"Violates foreign key constraint because key \"id: 999\" does not exist in the referenced table"
);
}
Err(e) => panic!("Expected foreign key constraint error, got: {e:?}"),
Ok(_) => panic!("Expected foreign key constraint error, but flush succeeded"),
}

Ok(())
}
}
Loading