Skip to content

Commit

Permalink
Check GENERATED column
Browse files Browse the repository at this point in the history
  • Loading branch information
gwenn committed May 11, 2024
1 parent 298f4d0 commit df637dc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/lexer/sql/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ fn auto_increment() {
#[test]
fn generated() {
expect_parser_err_msg(
b"CREATE TABLE x(a PRIMARY KEY AS ('id'));",
b"CREATE TABLE x(a PRIMARY KEY AS ('id'))",
"generated columns cannot be part of the PRIMARY KEY",
);
expect_parser_err_msg(
b"CREATE TABLE x(a AS ('id') DEFAULT '')",
"cannot use DEFAULT on a generated column",
)
}

Expand Down
6 changes: 5 additions & 1 deletion src/parser/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,7 @@ impl ColumnDefinition {
col_type.name = new_type.join(" ");
}
}
let (mut primary_key, mut generated) = (false, false);
let (mut primary_key, mut generated, mut default) = (false, false, false);
for constraint in &cd.constraints {
if let ColumnConstraint::PrimaryKey { auto_increment, .. } = &constraint.constraint {
if *auto_increment
Expand Down Expand Up @@ -1405,12 +1405,16 @@ impl ColumnDefinition {
}
} else if let ColumnConstraint::Generated { .. } = &constraint.constraint {
generated = true;
} else if let ColumnConstraint::Default(..) = &constraint.constraint {
default = true;
}
}
if primary_key && generated {
return Err(custom_err!(
"generated columns cannot be part of the PRIMARY KEY"
));
} else if default && generated {
return Err(custom_err!("cannot use DEFAULT on a generated column"));
}
columns.insert(col_name.clone(), cd);
Ok(())
Expand Down

0 comments on commit df637dc

Please sign in to comment.