#2530·larastan

PostgreSQL SET SCHEMA / RENAME migration via DB::statement breaks Eloquent property inference

Author: andrioacCreated Aug 12, 2026Updated Aug 12, 2026
Labelsbug
  • Larastan Version: 3.10.0
  • Laravel Version: 13.8.x
  • PHPStan Level: 8
  • Database: PostgreSQL

Description

We found a static-analysis mismatch when an existing Eloquent table is moved to another PostgreSQL schema and renamed in a later migration using raw DB::statement() SQL.

The Laravel application works correctly at runtime, including the Eloquent relationship and reads from the schema-qualified table. Larastan, however, reports the database-backed model properties as undefined after the move.

We investigated this because our model changed from etapas_censo to censo.etapas.

Original migration flow

The table was originally created using Laravel's Schema Builder:

php
Schema::create('etapas_censo', function (Blueprint $table): void {
    $table->id();
    $table->unsignedSmallInteger('exercicio');
    $table->unsignedSmallInteger('codigo_etapa');
    $table->string('descricao_etapa', 255);
});

A later PostgreSQL-specific migration performed:

php
DB::statement('CREATE SCHEMA IF NOT EXISTS censo');
DB::statement('ALTER TABLE public.etapas_censo SET SCHEMA censo');
DB::statement('ALTER TABLE censo.etapas_censo RENAME TO etapas');

The model then used:

php
final class EtapaCenso extends Model
{
    protected $table = 'censo.etapas';
}

Actual result

Access to an undefined property App\Models\EtapaCenso::$codigo_etapa.
Access to an undefined property App\Models\EtapaCenso::$descricao_etapa.

Investigation

Our first suspicion was the Eloquent belongsTo() relationship and the conventional etapa_censo_id foreign key.

Before changing the database architecture, we added a Feature test that exercised the actual relationship against PostgreSQL:

php
$mapeamento = MapeamentoEtapaCenso::query()
    ->with('etapaCenso')
    ->sole();

$etapaCenso = $mapeamento->etapaCenso;

The related model correctly resolved to:

censo.etapas

and both properties were successfully read:

php
$etapaCenso->codigo_etapa;
$etapaCenso->descricao_etapa;

This ruled out:

  • the Eloquent relationship;
  • the etapa_censo_id foreign key;
  • PostgreSQL schema resolution;
  • the schema-qualified $table;
  • and the Laravel runtime itself.

We then investigated Larastan 3.10.0.

ModelPropertyHelper uses the exact value returned by Model::getTable() to find the database columns associated with a model in Larastan's internal table map.

That table map is reconstructed from migrations by MigrationHelper and SchemaAggregator.

SchemaAggregator recognizes migration operations expressed through Laravel's Schema Builder, including operations such as:

php
Schema::create(...)
Schema::table(...)
Schema::drop(...)
Schema::rename(...)

In our case, Larastan originally learned the table and its columns from:

php
Schema::create('etapas_censo', ...)

so its internal representation contained the table under the key:

etapas_censo

The subsequent PostgreSQL-specific operations:

sql
ALTER TABLE public.etapas_censo SET SCHEMA censo
ALTER TABLE censo.etapas_censo RENAME TO etapas

were executed through DB::statement() and therefore were not reflected in Larastan's migration-derived table map.

At runtime PostgreSQL correctly had:

censo.etapas

and the Eloquent model correctly returned:

censo.etapas

but Larastan still had the columns associated with:

etapas_censo

This explains why the database and Laravel were correct while Larastan reported the properties as undefined.

Project-level solution we adopted

Our application is still pre-production, so we do not yet have immutable migrations that have already been executed in customer or production databases.

Because of that, instead of introducing a Larastan-specific workaround, we normalized the original migration history.

The schemas are now created before the academic tables:

php
DB::statement('CREATE SCHEMA IF NOT EXISTS bncc');
DB::statement('CREATE SCHEMA IF NOT EXISTS censo');

and the table is created directly in its canonical location:

php
Schema::create('censo.etapas', function (Blueprint $table): void {
    $table->id();
    $table->unsignedSmallInteger('exercicio');
    $table->unsignedSmallInteger('codigo_etapa_agregada');
    $table->string('descricao_etapa_agregada', 200);
    $table->unsignedSmallInteger('codigo_etapa');
    $table->string('descricao_etapa', 255);
    $table->timestamps();
});

The composite foreign key is also created directly against the canonical table:

php
$table->foreign(
    ['etapa_censo_id', 'exercicio'],
    'mapeamentos_etapa_censo_etapa_exercicio_foreign',
)
    ->references(['id', 'exercicio'])
    ->on('censo.etapas')
    ->restrictOnDelete();

The later migration containing:

sql
ALTER TABLE ... SET SCHEMA
ALTER TABLE ... RENAME TO

was removed.

The Eloquent model remains explicitly schema-qualified:

php
final class EtapaCenso extends Model
{
    protected $table = 'censo.etapas';
}

Result after the change

After rewriting the migration, we reran our complete GitHub Actions quality pipeline.

The following all succeeded:

  • php artisan migrate:fresh --seed on PostgreSQL;
  • PHP formatting;
  • Larastan/PHPStan static analysis;
  • backend unit tests;
  • backend Feature tests;
  • backend coverage;
  • frontend formatting;
  • ESLint;
  • TypeScript typecheck;
  • frontend tests;
  • frontend coverage;
  • production build.

No separate PostgreSQL connection was necessary.

No PHPStan stub was necessary.

No baseline or ignoreErrors rule was necessary.

Larastan did not need to be removed.

This also confirmed an important detail:

Schema-qualified model tables such as censo.etapas are not inherently a problem for Larastan.

When the migration contains:

php
Schema::create('censo.etapas', ...)

and the model contains:

php
protected $table = 'censo.etapas';

Larastan successfully infers the database-backed properties.

Why this still matters upstream

Our solution is practical only because the application is still pre-production.

Rewriting previously executed migrations is generally not an acceptable solution for an application already deployed to production.

A production application may legitimately have an old migration such as:

php
Schema::create('etapas_censo', ...);

and later need a new PostgreSQL migration that moves that existing table:

php
DB::statement('ALTER TABLE public.etapas_censo SET SCHEMA censo');
DB::statement('ALTER TABLE censo.etapas_censo RENAME TO etapas');

The database migration is valid and Laravel/Eloquent continue to work, but Larastan's migration-derived representation can become stale.

Would Larastan consider either:

  1. recognizing safe literal PostgreSQL operations such as ALTER TABLE ... SET SCHEMA and ALTER TABLE ... RENAME TO during migration scanning; or

  2. providing a supported configuration or extension point allowing a previously inferred table name to be mapped to a new schema-qualified table name?

Simply stripping the schema prefix would not be safe because PostgreSQL applications may legitimately contain both:

public.etapas
censo.etapas

as two distinct tables.

I can provide a minimal reproduction repository if that would be useful.