Categories
Common Errors

“Base table or view not found” when installing a new Magento 2

Chances are, you’re trying to install Magento 2 from a code base that includes a module with custom console commands (CLI commands).

Last Updated on 2020-12-17 by aeno

The error

When installing a fresh instance of Magento 2 you may get one of the following error messages:

[PDOException] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'magento.store_website' doesn't exist
Code language: plaintext (plaintext)

That’s weird, you say? Of course the table store_website doesn’t exist yet, because you were just trying to install Magento. So, where does that exception come from?

The problem

Chances are, you’re trying to install Magento 2 from a code base that includes a module with custom console commands (CLI commands). And these commands might declare a constructor dependency for one of these troublesome classes or interfaces:

  • \Magento\Framework\Stdlib\DateTime\TimezoneInterface

The solution

To proceed with a successful installation, you have two options:

  1. Either disable the module that uses problematic class dependencies in it’s CLI commands, run the Magento 2 installer and enable the module again.
  2. Or, even better, replace those dependencies with a proxy of themselves. This can either be done by directly appending \Proxy to the class name or by using a DI configuration:
<type name="ProblematicCommand"> <arguments> <argument name="timeZone" xsi:type="object">Magento\Framework\Stdlib\DateTime\TimezoneInterface\Proxy</argument> </arguments> </type>
Code language: HTML, XML (xml)

Bonus: The root cause

What’s so special about these classes, that depending on them in console commands breaks the Magento 2 installation CLI command with a “Base table or view not found” error?
The short answer is: It’s \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection!

The long answer: When depending on classes such as \Magento\Framework\Stdlib\DateTime\Timezone you indirectly depend on their dependencies. For this example we can trace it back to:

  1. \Magento\Framework\Stdlib\DateTime\TimezoneInterface
  2. \Magento\Framework\Stdlib\DateTime\Timezone
  3. \Magento\Framework\App\ScopeResolverInterface
  4. \Magento\Store\Model\Resolver\Website
  5. \Magento\Store\Model\StoreManagerInterface
  6. \Magento\Store\Model\StoreManager
  7. \Magento\Store\Api\WebsiteRepositoryInterface
  8. \Magento\Store\Model\WebsiteRepository
  9. \Magento\Store\Model\ResourceModel\Website\Collection
  10. \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection

Magento’s ObjectManager calls all the constructors of the classes in the dependency chain. In most cases this is no problem at all. But the constructor for AbstractCollection calls its method _initSelect() in order to prepare the collection SQL select statement:

protected function _initSelect() { $this->getSelect()->from(['main_table' => $this->getMainTable()]); return $this; }
Code language: PHP (php)

And this is the place where the Exception will be thrown. You can’t depend on anything that depends on a DB collection when Magento is not installed, yet.

One reply on ““Base table or view not found” when installing a new Magento 2”

Leave a Reply

Your email address will not be published.