Magento 2 – Base Table or View Not Found: 1146 Table SQLSTATE[42S02] Doesn’t Exist

magento2

error:  "Base table or view not found: 1146 Table"

when I run a collection query in my Magento 2 application I get the above message.

The problem is that the table does indeed exist. however, Magento 2 appears to add the name of my 'database' to the main table name. This is why the query is unable to locate the table.

If I am correct, it means that I need to remove the automatic creation of a table prefix by Magento

This is the collection query:

$accountTypesCollection = $this->_modelConstantAccounttypes->create();
                                    $accountTypesCollection  
                                    ->addFieldToFilter('brandId', 'barclays')
                                    ->addFieldToFilter('brandId', array('eq'=>'barclays'))
                                    ->addFieldToFilter('suppress', array('nlike' => '1'));
        $accountTypesCollection->addAttributeToSort('priority');
  
echo $accountTypesCollection->getSelect(); 

This is the full printout of the select query:

SELECT `main_table`.* FROM `constants_account_types` AS `main_table` WHERE (`brandId` = 'barclays') AND (`brandId` = 'barclays') AND (`suppress` NOT LIKE '1') ORDER BY priority ASC

when I run this query directly into MySQL admin it returns the correct values.

so, I am unclear why Magento adds the name of the database to the query.

this is the full error :

Base table or view not found: 1146 Table 'magentotwo.constants_account_types' doesn't exist, query was: SELECT `main_table`.* FROM `constants_account_types` AS `main_table` WHERE (`brandId` = 'barclays') AND (`brandId` = 'barclays') AND (`suppress` NOT LIKE '1') ORDER BY priority ASC

this is my database config file:

/<Magento Install Dir>/app/etc/env.php

 'db' => 
  array (
    'table_prefix' => '',
    'connection' => 
    array (
      'default' => 
      array (
        'host' => 'localhost',
        'dbname' => 'magentotwo',
        'username' => 'root',
        'password' => '',
        'active' => '1',
      ),
    ),
  ),

you can see that the table prefix column is left blank.

Best Answer

The constants_account_types table does not exist in magentotwo DataBase so either table is not created or the wrong DataBase is configured in Magento.

Related Topic