refactor: relationship model to column lists for compound FK (ADR-0043)
Move the FK column fields String->Vec<String> through all six layers (AddRelationship/SqlForeignKey AST, RelationshipSchema, metadata, project.yaml, ReadForeignKey, RelationshipEnd). Metadata stores comma-joined lists in the existing TEXT cells; project.yaml endpoints now columns: [a, b] (house style). Executor logic is multi-column ready: resolve_fk_parent_columns (full-PK F-A + auto-expand F-D), per-pair type-compat, schema_to_ddl multi-column emission, pragma FK read grouped by id, auto-name + --create-fk per-column, multi-column teaching echo. Single-column behaviour preserved (one-element vecs); all 2181 tests green. The grammar to parse multi-column input lands next.
This commit is contained in:
+22
-7
@@ -785,12 +785,24 @@ fn build_add_relationship(path: &MatchedPath, _source: &str) -> Result<Command,
|
||||
.iter()
|
||||
.any(|i| matches!(&i.kind, MatchedKind::Flag("create-fk")));
|
||||
|
||||
// Collect every matched `parent_column` / `child_column` ident, in
|
||||
// order — one each for the single-column `from P.col to C.col`
|
||||
// form, or the full lists for the parenthesized compound form
|
||||
// `from P.(a, b) to C.(x, y)` (ADR-0043).
|
||||
let parent_columns = collect_idents(path, "parent_column");
|
||||
let child_columns = collect_idents(path, "child_column");
|
||||
if parent_columns.is_empty() || child_columns.is_empty() {
|
||||
return Err(ValidationError {
|
||||
message_key: "parse.error_wrapper",
|
||||
args: vec![("detail", "a relationship needs both endpoints".to_string())],
|
||||
});
|
||||
}
|
||||
Ok(Command::AddRelationship {
|
||||
name: ident(path, "relationship_name").map(str::to_string),
|
||||
parent_table: require_ident(path, "parent_table")?,
|
||||
parent_column: require_ident(path, "parent_column")?,
|
||||
parent_columns,
|
||||
child_table: require_ident(path, "child_table")?,
|
||||
child_column: require_ident(path, "child_column")?,
|
||||
child_columns,
|
||||
on_delete: on_delete.unwrap_or_else(ReferentialAction::default_action),
|
||||
on_update: on_update.unwrap_or_else(ReferentialAction::default_action),
|
||||
create_fk,
|
||||
@@ -1680,9 +1692,12 @@ where
|
||||
}
|
||||
SqlForeignKey {
|
||||
name,
|
||||
child_column,
|
||||
// Single-column for now; the parenthesized multi-column parse
|
||||
// (`FOREIGN KEY (a, b) REFERENCES P(x, y)`) lands with the
|
||||
// grammar-node change (ADR-0043).
|
||||
child_columns: vec![child_column],
|
||||
parent_table,
|
||||
parent_column,
|
||||
parent_columns: parent_column.map(|c| vec![c]),
|
||||
on_delete,
|
||||
on_update,
|
||||
}
|
||||
@@ -3202,9 +3217,9 @@ mod sql_alter_table_tests {
|
||||
assert_eq!(name, None);
|
||||
match *constraint {
|
||||
TableConstraint::ForeignKey(fk) => {
|
||||
assert_eq!(fk.child_column, "pid");
|
||||
assert_eq!(fk.child_columns, vec!["pid".to_string()]);
|
||||
assert_eq!(fk.parent_table, "P");
|
||||
assert_eq!(fk.parent_column.as_deref(), Some("id"));
|
||||
assert_eq!(fk.parent_columns, Some(vec!["id".to_string()]));
|
||||
}
|
||||
other => panic!("expected ForeignKey, got {other:?}"),
|
||||
}
|
||||
@@ -3216,7 +3231,7 @@ mod sql_alter_table_tests {
|
||||
assert_eq!(name.as_deref(), Some("fk_p"));
|
||||
match *constraint {
|
||||
TableConstraint::ForeignKey(fk) => {
|
||||
assert_eq!(fk.parent_column, None, "bare reference resolves at execution");
|
||||
assert_eq!(fk.parent_columns, None, "bare reference resolves at execution");
|
||||
}
|
||||
other => panic!("expected ForeignKey, got {other:?}"),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user