extend test for inline relations: test for different on delete criteria for required and optional relations
@@ -539,29 +539,54 @@ fn adding_an_inline_relation_must_result_in_a_foreign_key_in_the_model_table() {
model A {
id Int @id
b B @relation(references: [id])
+ c C? @relation(references: [id])
}
model B {
id Int @id
}
+
+ model C {
+ id Int @id
+ }
"#;
let result = dbg!(infer_and_apply(test_setup, api, &dm1).sql_schema);
let table = result.table_bang("A");
- let column = table.column_bang("b");
- assert_eq!(column.tpe.family, ColumnTypeFamily::Int);
+
+ let b_column = table.column_bang("b");
+ assert_eq!(b_column.tpe.family, ColumnTypeFamily::Int);
+ assert_eq!(b_column.arity, ColumnArity::Required);
+
+ let c_column = table.column_bang("c");
+ assert_eq!(c_column.tpe.family, ColumnTypeFamily::Int);
+ assert_eq!(c_column.arity, ColumnArity::Nullable);
+
assert_eq!(
table.foreign_keys,
- &[ForeignKey {
+ &[
+ ForeignKey {
constraint_name: match test_setup.sql_family {
SqlFamily::Postgres => Some("A_b_fkey".to_owned()),
SqlFamily::Mysql => Some("A_ibfk_1".to_owned()),
SqlFamily::Sqlite => None,
},
- columns: vec![column.name.clone()],
+ columns: vec![b_column.name.clone()],
referenced_table: "B".to_string(),
referenced_columns: vec!["id".to_string()],
- on_delete_action: ForeignKeyAction::Restrict,
- }]
+ on_delete_action: ForeignKeyAction::Restrict, // required relations can't set ON DELETE SET NULL
+ },
+ ForeignKey {
+ constraint_name: match test_setup.sql_family {
+ SqlFamily::Postgres => Some("A_c_fkey".to_owned()),
+ SqlFamily::Mysql => Some("A_ibfk_2".to_owned()),
+ SqlFamily::Sqlite => None,
+ },
+ columns: vec![c_column.name.clone()],
+ referenced_table: "C".to_string(),
+ referenced_columns: vec!["id".to_string()],
+ on_delete_action: ForeignKeyAction::SetNull,
+ }
+ ]
);
});
}
