Overview
Theinsert-without-columns rule flags INSERT statements that don’t specify an explicit column list. Inserting values without naming columns creates fragile code that breaks when table schemas change.
Rule name: insert-without-columns
Default severity: Warning
When This Rule Triggers
This rule triggers when:INSERT INTO table VALUES (...)is used without a column listINSERT INTO table SELECT ...is used without a column list
INSERT INTO table (col1, col2) VALUES (...)specifies columnsINSERT INTO table (col1, col2) SELECT ...specifies columnsINSERT INTO table DEFAULT VALUESis used (no column list needed)
Why It Matters
Schema Fragility
When you insert without a column list, you implicitly depend on:- The current order of columns in the table
- The current number of columns in the table
- The exact column positions
- Your
INSERTmay fail with “column count mismatch” - Values may go into the wrong columns
- Your application may insert incorrect data silently
Example of the Problem
Examples
Problematic: INSERT VALUES without columns
Problematic: INSERT VALUES without columns
Problematic: INSERT SELECT without columns
Problematic: INSERT SELECT without columns
users and temp. Schema changes break this.Good: INSERT VALUES with columns
Good: INSERT VALUES with columns
Good: INSERT SELECT with columns
Good: INSERT SELECT with columns
Allowed: INSERT DEFAULT VALUES
Allowed: INSERT DEFAULT VALUES
Good: Partial column list
Good: Partial column list
Best Practices
Always List Columns
Make it a habit to specify columns, even if you’re providing all of them:Benefits of Explicit Columns
- Self-documenting: Readers can see what each value represents
- Robust to schema changes: Adding columns doesn’t break the query
- Flexible ordering: You can list columns in any order
- Partial inserts: You can omit columns to use defaults
- Easier refactoring: Tools can track column usage
Multi-Row Inserts
With explicit columns, multi-row inserts are clearer:INSERT … SELECT Pattern
ForINSERT ... SELECT, always list columns on both sides:
When DEFAULT VALUES is Appropriate
INSERT ... DEFAULT VALUES is useful when:
- All columns have sensible defaults
- You’re creating a “blank” row to get an auto-generated ID
- You’ll update the row with actual values later
Implementation Details
The rule works by:- Checking if the statement is an
InsertStmt - Checking if the
Colslist is empty (no column list specified) - Checking if
SelectStmtexists (notDEFAULT VALUES) - Reporting a warning at the start of the statement
insert_without_columns.go