Skip to content

Commit

Permalink
Postgres dialect: Fix parse error for "on delete", "on update" clause…
Browse files Browse the repository at this point in the history
…s in column constraints (sqlfluff#1586)

* Issue sqlfluff#1554: Postgres dialect: Parse error for "on delete" in column constraint

* Fix for UPDATE keyword and update tests

Co-authored-by: Alan Cruickshank <alanmcruickshank@gmail.com>
Co-authored-by: Barry Pollard <barry@tunetheweb.com>
  • Loading branch information
3 people authored and ttomasz committed Oct 12, 2021
1 parent 6895869 commit 5afea25
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/sqlfluff/dialects/dialect_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,12 @@ class ColumnConstraintSegment(BaseSegment):
Ref("ColumnReferenceSegment"),
# Foreign columns making up FOREIGN KEY constraint
Ref("BracketedColumnReferenceListGrammar", optional=True),
Sequence(
"ON",
OneOf("DELETE", "UPDATE"),
Ref("ReferentialActionSegment"),
optional=True,
),
),
),
OneOf("DEFERRABLE", Sequence("NOT", "DEFERRABLE"), optional=True),
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/parser/postgres/postgres_create_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,13 @@ operation_id int4 NOT NULL DEFAULT '-1'::integer
CREATE TABLE main.test_table (
"col1" character varying(40) NOT NULL
);

CREATE TABLE groups (
group_id INTEGER PRIMARY KEY generated BY DEFAULT AS IDENTITY
);

CREATE TABLE users (
user_id INTEGER PRIMARY KEY generated BY DEFAULT AS IDENTITY,
group_id INTEGER REFERENCES groups (group_id) ON DELETE CASCADE,
domain_id INTEGER REFERENCES groups (group_id) ON UPDATE RESTRICT
);
84 changes: 83 additions & 1 deletion test/fixtures/parser/postgres/postgres_create_table.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# computed by SQLFluff when running the tests. Please run
# `python test/generate_parse_fixture_yml.py` to generate them after adding or
# altering SQL files.
_hash: 0ef79bbd602bc4202e929d6764458043407d864a13ed41260627d3ad66420b22
_hash: 463feff7c6b4deebbfc0bdd82d953eb4544d40a5e359724a4c64a6631ad6c37a
file:
- statement:
create_table_statement:
Expand Down Expand Up @@ -1233,3 +1233,85 @@ file:
- keyword: 'NULL'
end_bracket: )
- statement_terminator: ;
- statement:
create_table_statement:
- keyword: CREATE
- keyword: TABLE
- table_reference:
identifier: groups
- bracketed:
- start_bracket: (
- column_reference:
identifier: group_id
- data_type:
data_type_identifier: INTEGER
- column_constraint_segment:
- keyword: PRIMARY
- keyword: KEY
- column_constraint_segment:
- keyword: generated
- keyword: BY
- keyword: DEFAULT
- keyword: AS
- keyword: IDENTITY
- end_bracket: )
- statement_terminator: ;
- statement:
create_table_statement:
- keyword: CREATE
- keyword: TABLE
- table_reference:
identifier: users
- bracketed:
- start_bracket: (
- column_reference:
identifier: user_id
- data_type:
data_type_identifier: INTEGER
- column_constraint_segment:
- keyword: PRIMARY
- keyword: KEY
- column_constraint_segment:
- keyword: generated
- keyword: BY
- keyword: DEFAULT
- keyword: AS
- keyword: IDENTITY
- comma: ','
- column_reference:
identifier: group_id
- data_type:
data_type_identifier: INTEGER
- column_constraint_segment:
- keyword: REFERENCES
- column_reference:
identifier: groups
- bracketed:
start_bracket: (
column_reference:
identifier: group_id
end_bracket: )
- keyword: 'ON'
- keyword: DELETE
- referential_action:
keyword: CASCADE
- comma: ','
- column_reference:
identifier: domain_id
- data_type:
data_type_identifier: INTEGER
- column_constraint_segment:
- keyword: REFERENCES
- column_reference:
identifier: groups
- bracketed:
start_bracket: (
column_reference:
identifier: group_id
end_bracket: )
- keyword: 'ON'
- keyword: UPDATE
- referential_action:
keyword: RESTRICT
- end_bracket: )
- statement_terminator: ;

0 comments on commit 5afea25

Please sign in to comment.