@@ -176,7 +176,7 @@ impl fmt::Debug for Appender<'_> {
176
176
177
177
#[ cfg( test) ]
178
178
mod test {
179
- use crate :: { Connection , Result } ;
179
+ use crate :: { params , Connection , Error , Result } ;
180
180
181
181
#[ test]
182
182
fn test_append_one_row ( ) -> Result < ( ) > {
@@ -264,7 +264,6 @@ mod test {
264
264
#[ test]
265
265
#[ cfg( feature = "chrono" ) ]
266
266
fn test_append_datetime ( ) -> Result < ( ) > {
267
- use crate :: params;
268
267
use chrono:: { NaiveDate , NaiveDateTime } ;
269
268
270
269
let db = Connection :: open_in_memory ( ) ?;
@@ -285,8 +284,7 @@ mod test {
285
284
}
286
285
287
286
#[ test]
288
- fn test_appender_error ( ) -> Result < ( ) , crate :: Error > {
289
- use crate :: params;
287
+ fn test_appender_error ( ) -> Result < ( ) > {
290
288
let conn = Connection :: open_in_memory ( ) ?;
291
289
conn. execute (
292
290
r"CREATE TABLE foo (
@@ -298,12 +296,44 @@ mod test {
298
296
) ?;
299
297
let mut appender = conn. appender ( "foo" ) ?;
300
298
match appender. append_row ( params ! [ "foo" ] ) {
301
- Err ( crate :: Error :: DuckDBFailure ( .., Some ( msg) ) ) => {
299
+ Err ( Error :: DuckDBFailure ( .., Some ( msg) ) ) => {
302
300
assert_eq ! ( msg, "Call to EndRow before all columns have been appended to!" )
303
301
}
304
302
Err ( err) => panic ! ( "unexpected error: {err:?}" ) ,
305
303
Ok ( _) => panic ! ( "expected an error but got Ok" ) ,
306
304
}
307
305
Ok ( ( ) )
308
306
}
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
+ }
309
339
}
0 commit comments