[TSQL] LT01 error with in-line primary key declaration
svengiegerich opened this issue · comments
Search before asking
- I searched the issues and found no similar issues.
What Happened
When trying to fix a query that specifies the PRIMARY KEY
in the column line, I run into the assertion error below.
Expected Behaviour
As the query is valid in MSSQL, I would have expected sqlfluff
to lint (fix) accordingly.
Observed Behaviour
==== finding fixable violations ====
WARNING One fix for LT01 not applied, it would re-cause the same error.
WARNING Unable to lint sick_pay_core/analytics_pipeline/tbl/test.sql due to an internal error. Please report this as an issue with your query's contents and stacktrace below!
To hide this warning, add the failing file to .sqlfluffignore
Traceback (most recent call last):
File "/Users/svengiegerich/opt/miniconda3/envs/sick-pay-core/lib/python3.11/site-packages/sqlfluff/core/linter/runner.py", line 110, in run
yield partial()
^^^^^^^^^
File "/Users/svengiegerich/opt/miniconda3/envs/sick-pay-core/lib/python3.11/site-packages/sqlfluff/core/linter/linter.py", line 681, in lint_rendered
return cls.lint_parsed(
^^^^^^^^^^^^^^^^
File "/Users/svengiegerich/opt/miniconda3/envs/sick-pay-core/lib/python3.11/site-packages/sqlfluff/core/linter/linter.py", line 655, in lint_parsed
formatter.dispatch_file_violations(
File "/Users/svengiegerich/opt/miniconda3/envs/sick-pay-core/lib/python3.11/site-packages/sqlfluff/cli/formatters.py", line 262, in dispatch_file_violations
s = self._format_file_violations(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/svengiegerich/opt/miniconda3/envs/sick-pay-core/lib/python3.11/site-packages/sqlfluff/cli/formatters.py", line 241, in _format_file_violations
self.format_violation(
File "/Users/svengiegerich/opt/miniconda3/envs/sick-pay-core/lib/python3.11/site-packages/sqlfluff/cli/formatters.py", line 426, in format_violation
v_dict = violation.to_dict()
^^^^^^^^^^^^^^^^^^^
File "/Users/svengiegerich/opt/miniconda3/envs/sick-pay-core/lib/python3.11/site-packages/sqlfluff/core/errors.py", line 304, in to_dict
fixes=[fix.to_dict() for fix in self.fixes],
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/svengiegerich/opt/miniconda3/envs/sick-pay-core/lib/python3.11/site-packages/sqlfluff/core/errors.py", line 304, in <listcomp>
fixes=[fix.to_dict() for fix in self.fixes],
^^^^^^^^^^^^^
File "/Users/svengiegerich/opt/miniconda3/envs/sick-pay-core/lib/python3.11/site-packages/sqlfluff/core/rules/fix.py", line 149, in to_dict
assert len(self.edit[0].source_fixes) == 1
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError
How to reproduce
This does not work,
CREATE TABLE foo
(
x INT NOT NULL PRIMARY KEY
, y INT NULL
, z INT NULL
);
If I move the PRIMARY KEY
in a separate line, sqlfluff
can fix it
CREATE TABLE foo
(
x INT NOT NULL
, y INT NULL
, z INT NULL
, PRIMARY KEY (x)
);
Same if I remove the NOT NULL
CREATE TABLE foo
(
x INT PRIMARY KEY
, y INT NULL
, z INT NULL
);
Dialect
TSQL
Version
3.0.0
Configuration
[sqlfluff]
dialect = tsql
exclude_rules = layout.cte_newline,layout.long_lines,aliasing.forbid,structure.subquery,ambiguous.column_count,aliasing.length,structure.column_order,structure.join_condition_order
large_file_skip_byte_limit = 100000
max_line_length = 120
encoding = utf-8
#--------------------------------------------------------------------------------------------------
# LAYOUT CONFIGURATION
#--------------------------------------------------------------------------------------------------
# >>> General
[sqlfluff:indentation]
allow_implicit_indents = true
indented_on_contents = false
indented_then = false
indented_then_contents = false
indented_using_on = true
# <<< General
# >>> Commas
[sqlfluff:layout:type:comma]
spacing_before = touch
line_position = leading
# <<< Commas
# >>> CTEs
[sqlfluff:layout:type:common_table_expression]
spacing_within = single
# <<< CTEs
# >>> Table definitions
[sqlfluff:layout:type:column_constraint_segment]
spacing_before = align
align_within = create_table_statement
[sqlfluff:layout:type:data_type]
spacing_before = align
align_within = create_table_statement
# <<< Table definitions
# >>> FROM clauses
[sqlfluff:layout:type:post_table_expression]
spacing_within = touch:inline
[sqlfluff:layout:type:query_hint_segment]
spacing_before = touch:inline
spacing_after = touch:inline
[sqlfluff:layout:type:from_clause]
spacing_within = single:inline
# <<< FROM clauses
#--------------------------------------------------------------------------------------------------
# RULE CONFIGURATION
#--------------------------------------------------------------------------------------------------
[sqlfluff:rules:capitalisation.keywords]
capitalisation_policy = upper
[sqlfluff:rules:capitalisation.identifiers]
extended_capitalisation_policy = lower
ignore_words = hospitals_full_rates_I68_backup, hospitals_full_rates_I68
[sqlfluff:rules:capitalisation.functions]
extended_capitalisation_policy = upper
[sqlfluff:rules:capitalisation.literals]
capitalisation_policy = upper
[sqlfluff:rules:structure.subquery]
forbid_subquery_in = both
[sqlfluff:rules:capitalisation.types]
extended_capitalisation_policy = upper
[sqlfluff:rules:references.keywords]
ignore_words = data_source, data, name, year, month
Are you willing to work on and submit a PR to address the issue?
- Yes I am willing to submit a PR!
Code of Conduct
- I agree to follow this project's Code of Conduct
This is odd, I just ran into a similar issue with the postgres
dialect. The following causes the same issue as above when calling fix
(although there is nothing to fix):
CREATE TABLE table1 (
a TEXT PRIMARY KEY,
b TEXT NOT NULL,
c TEXT NOT NULL REFERENCES table2 (c) ON DELETE RESTRICT
);
The culprit seems to be the last column constraint, namely, REFERENCES table2 (c) ON DELETE RESTRICT
.