Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR will:
I hope I'll be able to understandably present the issue which is caused by defining
getConnectionName
in Sushi traited models. I've added a test that showcases the issue, but it warrants an explanation why it's done this way as well.To start from beginning - recently after updating Sushi to newest version I've noticed a fatal error that was caused by trying to access an Eloquent model through a relation with Sushi model. This is showcased in test and two models - in my system Quz is saved to database, but references Qux which exists in filesystem (e.g. Quz will be saved in the DB as
['title' => 'foo', 'qux_id' => 1]
)From debugging (and my understanding) the connection that Laravel uses propagates through relationship, if it's defined on the models - ie. trying to access
Qux::find(1)->quz
will causeconnection
on Quz to be taken from Qux. As such, instead of using the default one (in my case psql), it will useSqlLiteConnection
instead, as that's theconnection
that will be resolved usinggetConnectionName
method.The test showcases this - by default, default connection should be used (orchestra sets mysql as default one). This connection doesn't exist, so exception with
Connection refused
message should be raised. The erroneous behaviour of using sqlite file results inno such table: quzs
exception message instead.If
getConnectionName
method doesn't exist (or returns null) on Sushi trait, Laravel will correctly resolve defaultconnection
class. I don't know why this happens only ifgetConnectionName
is defined on one of the related models, but not the other; whenconnectionName
is set on Eloquent model, correctconnection
class is resolved, even when one ispsql
and the other isstatic::class
.As such the below works:
This works as well:
Unfortunately this requires all Eloquent models that have a relationship with Sushi model to define
connectionName
explicitly - otherwise accessing given model through relationship will result in error.To be fair, I'd prefer the current code to remain, as it elegantly resolves the exists validation. However, I feel that majority of users will never explicitly define connection name on their models and as such will experience this error. A solution might well be to warn users about the danger in the readme.