<!-- Source: @site/projects/sqlens-for-laravel/docs/rules/pg-l4-not-null-without-default.md -->
<!-- Canonical: https://docs.pushery.com/sqlens-for-laravel/rules/pg-l4-not-null-without-default/ -->

# PG.L4.NOT_NULL_WITHOUT_DEFAULT — The previous release writes rows without the column

> A column that refuses NULL and has no default fails every INSERT of the old release that leaves it out.

- **Category:** safety
- **Level:** 4
- **Confidence:** heuristic
- **Downtime class:** `online`
- **Stability:** preview
- **Suites:** lint
- **Applies to:** PostgreSQL 18

`PG.L2.SET_NOT_NULL_SCAN` asks how long `SET NOT NULL` holds its lock while it scans the table. This
rule asks who writes the column. Between `migrate` and the switch, the previous release serves the
migrated schema: for seconds on every deploy, and until a deploy comes through when a step after
`migrate` fails. That release does not know the column and leaves it out of every `INSERT`. Where
the column refuses NULL and has no default, PostgreSQL answers each of those statements with
`23502`.

## What it reads

Three statements reach that state, each on a table an earlier migration created:

- `ADD COLUMN … NOT NULL` with no default, generated value, identity or serial type. On a table
  that holds rows PostgreSQL refuses the statement itself, and on an empty one the release before it
  breaks.
- `SET NOT NULL` on a column that took NULL until then, where no default is left after the
  statement.
- `DROP DEFAULT` on a column that had one, where it still refuses NULL after the statement.

The last two need to know what the column was before, and the rule reads that from the statements
of the run that created and changed it. Laravel's `->change()` restates every modifier, so widening a
`varchar` writes `SET NOT NULL` and `DROP DEFAULT` whether or not either changes anything. Where the
run does not say what the column was, as in a `--file` run, the rule stays silent. What it does
report of `->change()` is real: a column that kept a default loses it whenever `->default()` is left
off.

## Why `heuristic`

The rule cannot see your deploy process or your code. A project that stops all traffic, migrates and
then starts the new release has no overlap window, and a column the old release already writes
breaks nothing. The finding names the risk. Whether it applies is something only your pipeline and
your code know.

## Flagged

```php
Schema::table('orders', fn (Blueprint $table) => $table->string('status', 20));
```

## Preferred

```php
Schema::table('orders', fn (Blueprint $table) => $table->string('status', 20)->default('pending'));
```

A default gives every row a value, the rows already in the table and the ones the previous release
inserts. Where no default fits, add the column nullable, ship a release that writes it, and set
`NOT NULL` in a later deploy. With `->change()`, state the default again, or the column loses it.

## Sources

- [PostgreSQL 18 — `ALTER TABLE`](https://www.postgresql.org/docs/18/sql-altertable.html) — a
  column added with no constraint takes NULL as its default, and `SET NOT NULL` may only be applied
  to a column none of whose rows holds NULL
- [PostgreSQL 18 — Appendix A, error codes](https://www.postgresql.org/docs/18/errcodes-appendix.html)
  — `23502` is `not_null_violation`

<!-- BEGIN generated: remediation -->
## The fix material this rule carries

A finding from this rule carries a payload whose strategy is `none`: this rule has looked,
and there is no safe standard sequence. That is a conclusion rather than an omission — a
finding with no payload at all says only that nobody wrote one.

The payload is material for you or an agent to apply. SQLens writes no migration and runs no
DDL. See [the remediation payload](https://docs.pushery.com/sqlens-for-laravel/remediation-schema.md) for every field, the placeholder
semantics, and the version rules a consumer has to follow.
<!-- END generated: remediation -->
