larastan icon indicating copy to clipboard operation
larastan copied to clipboard

Undefined property on models when using postgres schema

Open dododedodonl opened this issue 2 years ago • 6 comments

  • Larastan Version: 2.6.0
  • --level used: 5

Description

In postgres, it is possible to use multiple schemas/search paths in a single database connection. In order to access some tables in a different schema then the default ones set in the laravel database config, they are prefixed to the table name in getTable() on the specific models (eg. anotherSchema.tableName instead of tableName).

It seems larastan cannot match the tablenames anymore to the migrations, and this results in undefined property errors when running phpstan.

dododedodonl avatar May 10 '23 08:05 dododedodonl

It seems larastan cannot match the tablenames anymore to the migrations

Yes. It is a missing feature.

szepeviktor avatar May 10 '23 08:05 szepeviktor

Seems like duplicate of #1577

canvural avatar Jun 04 '23 08:06 canvural

@canvural it is not a duplicate, as only 1 database connection is used. Postgres databases are split into schema’s (sub databases), which can be accessed by adding it in front of the table. Larastan is not correctly interpreting this, and accessing all tables from the main (public) schema in the database.

dododedodonl avatar Jun 04 '23 08:06 dododedodonl

@dododedodonl,

As long as the Models are declaring the schema as part of the table name:

class Invoice extends Model
{
    protected $table = 'accounting.invoices';
}

and migrations are using the schema:

    {
        Schema::create('accounting.invoices', function (Blueprint $table) {
            $table->id();
            $table->string('invoice_number');
            $table->date('invoice_date');
            $table->timestamps();
        });
    }

then Larastan should work without an issue.

Is this still a problem? If so, then I'll need an mvp to troubleshoot. Are you using squashed migrations / schema dump or only reading the migrations?

calebdw avatar Mar 10 '24 19:03 calebdw

Both the models and the migrations are specifying the schema. Only difference from your example I see is that I use the getTable() method to set the schema. I'll try to make a repo to reproduce the issue one of the coming days.

dododedodonl avatar Mar 11 '24 11:03 dododedodonl

Only difference from your example I see is that I use the getTable() method to set the schema.

That shouldn't matter though because Larastan directly calls the model instance's getTable method

calebdw avatar Mar 11 '24 13:03 calebdw