Skip to content

Commit 6523dab

Browse files
authored
Allow foreign table constraint without columns (#1608)
1 parent fac84d8 commit 6523dab

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

src/ast/ddl.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -885,12 +885,14 @@ impl fmt::Display for TableConstraint {
885885
} => {
886886
write!(
887887
f,
888-
"{}FOREIGN KEY ({}) REFERENCES {}({})",
888+
"{}FOREIGN KEY ({}) REFERENCES {}",
889889
display_constraint_name(name),
890890
display_comma_separated(columns),
891891
foreign_table,
892-
display_comma_separated(referred_columns),
893892
)?;
893+
if !referred_columns.is_empty() {
894+
write!(f, "({})", display_comma_separated(referred_columns))?;
895+
}
894896
if let Some(action) = on_delete {
895897
write!(f, " ON DELETE {action}")?;
896898
}

src/parser/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6830,7 +6830,7 @@ impl<'a> Parser<'a> {
68306830
let columns = self.parse_parenthesized_column_list(Mandatory, false)?;
68316831
self.expect_keyword(Keyword::REFERENCES)?;
68326832
let foreign_table = self.parse_object_name(false)?;
6833-
let referred_columns = self.parse_parenthesized_column_list(Mandatory, false)?;
6833+
let referred_columns = self.parse_parenthesized_column_list(Optional, false)?;
68346834
let mut on_delete = None;
68356835
let mut on_update = None;
68366836
loop {

tests/sqlparser_common.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4167,6 +4167,7 @@ fn parse_alter_table_constraints() {
41674167
check_one("UNIQUE (id)");
41684168
check_one("FOREIGN KEY (foo, bar) REFERENCES AnotherTable(foo, bar)");
41694169
check_one("CHECK (end_date > start_date OR end_date IS NULL)");
4170+
check_one("CONSTRAINT fk FOREIGN KEY (lng) REFERENCES othertable4");
41704171

41714172
fn check_one(constraint_text: &str) {
41724173
match alter_table_op(verified_stmt(&format!(

0 commit comments

Comments
 (0)