I have defined an additional custom database connection in etc/env.php:

'db' => [
    'table_prefix' => 'abc_',
    'connection' => [
        'default' => [
            'host' => 'localhost',
            'dbname' => 'db1',
            'username' => 'db1',
            'password' => 'pw1',
            'active' => '1'
        'custom' => [
            'host' => 'localhost',
            'dbname' => 'db2',
            'username' => 'db2',
            'password' => 'pw2',
            'active' => '1'

Tables inside the default connection all have the prefix abc_. The tables in the custom connection don't have a prefix. Unfortunately the table_prefix option seems to apply to all defined connections.

Is there a way to have this setting on a connection basis?


I also faced this issue. It seems that there's no other solution to it aside from creating an around plugin on the getTableName

I would suggest you create an around plugin on Magento\Framework\App\ResourceConnection

Since the method getTableName, is the one responsible for adding in the table prefix.


replace 'table_prefix' => 'abc_' with 'table_prefix' => '' in env.php

  • One database HAS prefixes the other does not. But magento assumes the prefix for any connection. When I remove it the other DB is not accessible.
    – omsta
    Mar 8 '19 at 10:43
  • in your script try to add prefix for your custom data base table using $connection = $this->_resource->getConnection(); $tableName = $connection->getTableName('tablename_without_prefix'); Mar 8 '19 at 10:53
  • There is no way for me to change the current state. The magento DB has prefixes and the other does not. I cannot change that because it is beyond the scope of my project (customer decided). I am not using raw sql queries but resource models that obviously retrieve the prefix from the global setting.
    – omsta
    Mar 8 '19 at 10:56
  • can you please add your error here Mar 8 '19 at 10:58
  • "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db2.abc_lot' doesn't exist, query was: SELECT main_table.* FROM abc_lot AS main_table INNER JOIN abc_lotartikel AS a ON main_table.LOT_ID = a.LOT_ID GROUP BY a.LOT_ID". That database does not have prefixes so it is looking for the table "abc_lot" but there is only a table named "lot".
    – omsta
    Mar 8 '19 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.