Summer Sale! All accounts are 50% off this week.

Shipupi's avatar

Failing relationship when using refreshDatabase

Hello, I am running a setup with 2 different databases which are on the same server called 'accounts' and 'main'. For testing purposes, have 2 other databases called "test_accounts" and "test_main".

When defining the connections in database.php file, I've set it up as:

'connections' => [

        'main' => env("APP_ENV") !== 'testing'? $main : $test_main,

        'accounts' => env("APP_ENV") !== 'testing'? $accounts : $test_accounts,
    ],

There is a specific relationship which was problematic using this 2-connections setup. The accounts db has a table called 'user' and the main db has the tables 'record' and 'owner'.

Owner works as a pivot for a belongsToMany between user and record defined like this:

// Record.php
public function users()
    {
        $database = $this->getConnection()->getDatabaseName();    
        return $this->belongsToMany(User::class, $database . ".owner")
            ->as('owner');
    }

This was the only way I found out how to make the relationship work, otherwise it would fail, even if all of the relevant models a correctly defined $connection property.

So far this setup had been working fine, the issue came when I'm trying to run tests using refreshDatabase, once I enable the transactions with the following command:

protected $connectionsToTransact = ['accounts', 'main'];

The relationship fails inside the test:

dd(Record::first()->users)

Returns nulls. But it returns a user if I remove the $connectionsToTransact property.

The reverse direction for the relation works, in both cases, with and without the transactions enabled. If I try on the same test

dd(User::first()->records)

I get the record, so the entities are there it's just the relationship that breaks.

0 likes
0 replies

Please or to participate in this conversation.