Skip to content

Commit

Permalink
Check foreign key constraint on column definition
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed Mar 17, 2024
1 parent 7edcf6b commit 327db8e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/lexer/sql/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ fn create_strict_table_unknown_datatype() {
);
}

#[test]
fn foreign_key_on_column() {
expect_parser_err_msg(
b"CREATE TABLE t(a REFERENCES o(a,b))",
"foreign key on a should reference only one column of table o",
);
}

#[test]
fn create_strict_table_generated_column() {
parse_cmd(
Expand Down
18 changes: 18 additions & 0 deletions src/parser/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1148,6 +1148,24 @@ impl ColumnDefinition {
col_type.name = new_type.join(" ");
}
}
for constraint in &cd.constraints {
if let ColumnConstraint::ForeignKey {
clause:
ForeignKeyClause {
tbl_name, columns, ..
},
..
} = &constraint.constraint
{
if columns.as_ref().map_or(0, |v| v.len()) != 1 {
return Err(custom_err!(
"foreign key on {} should reference only one column of table {}",
col_name,
tbl_name
));
}
}
}
columns.insert(col_name, cd);
Ok(())
}
Expand Down

0 comments on commit 327db8e

Please sign in to comment.