-
Notifications
You must be signed in to change notification settings - Fork 601
Allow foreign table constraint without columns #1608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow foreign table constraint without columns #1608
Conversation
Postgres makes skipping the `(referenced-columns)` part in `ALTER TABLE ... FOREIGN KEY .. REFERENCES <tablename> (referenced-columns)` per https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-PARMS-REFERENCES. Since the generic SQL grammar requires referenced columns (https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#referential-constraint-definition), this change applies only to Postgres.
An alternative design would be to add a new method in the |
src/parser/mod.rs
Outdated
let referred_columns = self.parse_parenthesized_column_list(Mandatory, false)?; | ||
// PostgreSQL allows foreign key columns to be optional | ||
// (https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-PARMS-REFERENCES) | ||
let parenthesized_column_list_optional = if dialect_of!(self is PostgreSqlDialect) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this behavior is a superset of the previous I think we can do without the dialect check and always set to Optional
?
Also could we add a test case to sqlparse_common.rs demonstrating the new behavior?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Made the change.
Also, fixes DDL generation when reference columns aren't specified and adds a test.
Our migration tests have used strings in Rust files (for exo and expected SQL), which required string escaping, prefix stripping, and other modifications. This made it difficult to read and extend the test code (for example, consider `MigrationScope`s). Also, we relied on exact string matches, which required paying too much attention to the precise format. This refactoring: 1. Move .exo and .sql files outside of rust code 2. Use `sqlparser` to match without considering the format Note that in some cases, `sqlparser` doesn't parse valid syntax, so we rely on exact string comparison as a fallback/optimization. We have already contributed a couple of changes (apache/datafusion-sqlparser-rs#1608, apache/datafusion-sqlparser-rs#1610) and will continue to do so to remove this reliance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for keeping up the review train @iffyio -- being part of the unsung heros of the Rust data ecosystem 🦾 |
Postgres allows skipping the
(referenced-columns)
part inALTER TABLE ... FOREIGN KEY .. REFERENCES <tablename> (referenced-columns)
per https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-PARMS-REFERENCES.Since the generic SQL grammar requires referenced columns (https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#referential-constraint-definition), this change applies only to Postgres.