Sqlfluff rule L025 breaks due to postgres `generate_series()`

207 views Asked by At

I'm using sqlfluff to lint my postges code. I'm down to a single linting error, in my test code, that I dont know how to fix by adjusting my sql nor how to configure it.

The rules im breaking is L025.
The issue is that sqlfluff thinks, i'm not using the alias i because I dont write womething like i.value.

With the following code:

insert into private.account (
  account_first_name,
  account_last_name,
  account_display_name,
  account_email,
  account_password
)
select
  'Random' as account_first_name,
  'User' as account_last_name,
  concat('Random User ', i) as account_display_name,
  concat('random', i, '@email.com') as account_email,
  gen_random_hash() as account_password
from generate_series(1, 200) as i;

and the coresponding error:

== [test\generate_accounts.sql] FAIL
 L: 38 | P: 33 | L025 | Alias 'i' is never used in SELECT statement.
 All Finished!
1

There are 1 answers

0
AudioBubble On BEST ANSWER

I do agree with the linter here. i is a table alias and shouldn't be used as a column reference. It's cleaner to use

from generate_series(1, 200) as g(i)

or whatever naming you prefer.

Then reference the value as g.i