Skip to content

SQL · maintainability

prefer-text-over-varchar

sql:prefer-text-over-varchar

VARCHAR(n) — use TEXT (+ CHECK length if needed).

Code
SARJ104
Default
error
Fix
none
Languages
sql

Why

PostgreSQL gives VARCHAR(n) no storage or performance advantage, while its length cap obscures a business constraint.

Fix

Use TEXT and express a real maximum length with an explicit CHECK constraint when needed.

Before / after

Executed by this rule’s unit tests.

Before

Length-limited VARCHAR column

migrations/001_users.sql · focus
CREATE TABLE users (name VARCHAR(255) NOT NULL);

After

Text column with an explicit length constraint

migrations/001_users.sql · focus
CREATE TABLE users (name TEXT NOT NULL CHECK (char_length(name) <= 255));

Limits

  • MySQL and SQLite sources are excluded because their VARCHAR behavior differs.