sqlfluff not aligning create table constraints when multiple constraints are present

205 views Asked by At

I am using sqlfluff to format my postgres migrations and my queries. In the company I work at, we align our create table types and constraints.

When creating tables, the alignment between column constraints seems to fail when multiple constraints exist (i.e. not null unique vs not null). The documentation is quite opaque for table creation statements, so I have been stuck on this issue for a while.

When asked to align constraints, sqlfluff seems to align constraints on the last constraint (unique on not null unique), which I do not want - I want to align based on the first constraint (the not).

To use a toy example, the ideal formatting would be

create table tbl (
    foo   text not null,
    fooey int  not null,
    bar   text not null unique
);

However, I seem to get the following:

create table tbl (
    foo   text          not null,
    fooey int           not null,
    bar   text not null unique
);

I have found that when I insert a primary key generated always as identity, sqlfluff then seems to align everything on the generated and entirely ignore alignment for multiple constraints (?!)

create table tbl (
    id    text primary key generated always as identity,
    foo   text             not null,
    fooey int              not null,
    bar   text not null unique
);

Is this a bug or intended behaviour?

I've scoured the web for anyone else with this issue, yet I have not found any path to fixing it. My .sqlfluff is provided below, please help! Thank you.

[sqlfluff]
templater = jinja
dialect = postgres
max_line_length = 0
exclude_rules = None
processes = 1


[sqlfluff:indentation]
tab_space_size = 4
indent_unit = space
indented_using_on = true
indented_on_contents = true
allow_implicit_indents = true
indented_joins = true
indented_ctes = true

[sqlfluff:layout:type:column]
spacing_before = align
align_within = statement
align_scope = file

[sqlfluff:layout:type:column_definition]
spacing_before = align
align_within = statement
align_scope = file

[sqlfluff:layout:type:column_constraint_segment]
spacing_before = align
align_within = statement
align_scope = file

[sqlfluff:layout:type:data_type]
spacing_before = align
align_within = statement
align_scope = file
1

There are 1 answers

0
William M On

This was indeed a bug, and has since been resolved in PR 5238. There are some other issues with alignment for the Postgres dialect at the moment (for instance square brackets), but it appears to be improving quickly.