ahoi's avatar
Level 5

AssertSoftDeleted on another connection fails

Hi there,

I am wondering why my assertion does not work if I use a different database connection. But I'll start at the beginning:

I got a model, which is stored using a different database connection called keys-db. The model itself uses

protected $connection = 'keys-db';

Now if I do this:

EncryptionKey::find(123);
assertSoftDeleted(table: $encryptionKey, connection: 'keys-db');

This fails with:

Illuminate\Database\QueryException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'example_test.encryption_keys' doesn't exist (Connection: mysql, SQL: select count(*) as aggregate from `encryption_keys` where (`id` = 9a7e0d0c-599e-4fdd-85a5-14ea694c02af) and `deleted_at` is not null)
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:416
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:788
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:755
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:407
at vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2751
at vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2740
at vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:3294
at vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2739
at vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:3221
at vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:3149
at vendor/laravel/framework/src/Illuminate/Testing/Constraints/SoftDeletedInDatabase.php:66
at vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php:108
at vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php:100
at vendor/pestphp/pest-plugin-laravel/src/Database.php:79
at tests/Feature/Domain/Vault/Actions/DeleteVaultTest.php:25
at vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestCase.php:174
Caused by
PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'example_test.encryption_keys' doesn't exist

Yeah, sure... It should be example_keys_test, as defined here:

        'keys-db' => [
            'driver'         => 'mysql',
            'url'            => env('DATABASE_KEYS_URL'),
            'host'           => env('DB_KEYS_HOST', '127.0.0.1'),
            'port'           => env('DB_KEYS_PORT', '3306'),
            'database'       => env('DB_KEYS_DATABASE', 'forge'),
            'username'       => env('DB_KEYS_USERNAME', 'forge'),
            'password'       => env('DB_KEYS_PASSWORD', ''),
            'unix_socket'    => env('DB_KEYS_SOCKET', ''),
            'charset'        => 'utf8mb4',
            'collation'      => 'utf8mb4_unicode_ci',
            'prefix'         => '',
            'prefix_indexes' => true,
            'strict'         => true,
            'engine'         => null,
            'options'        => extension_loaded('pdo_mysql') ? array_filter([
                                                                                 PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
                                                                             ]) : [],
        ],

And if I add

    ray(env('DB_KEYS_DATABASE', 'forge'), config('database.connections.keys-db.database'));

right into my test, I am getting the correct result:

example_keys_test
example_keys_test

What am I doing wrong?

P.S. Okay, I see: The first argument is the table, which should be example_keys_test. But shouldn't it automatically guess that, because $encryptionKey->getTable() returns this correctly...?

0 likes
2 replies
LaryAI's avatar
Level 58

The issue seems to be with the table name used in the assertSoftDeleted method. The method is looking for a table named encryption_keys in the example_test database, but the actual table name is example_keys_test in the keys-db database.

To fix this, you need to specify the correct table name and connection in the assertSoftDeleted method. Here's an example of how you can modify your code:

EncryptionKey::find(123);
assertSoftDeleted(table: 'example_keys_test', connection: 'keys-db');

Make sure to replace 'example_keys_test' with the actual table name in your database.

This should resolve the issue and allow the assertion to pass.

tisuchi's avatar

@ahoi How about this?

$encryptionKey = EncryptionKey::find(123);
$this->assertSoftDeleted('keys-db.encryption_keys', ['id' => $encryptionKey->id]);

Please or to participate in this conversation.