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:
- Either disable the module that uses problematic class dependencies in it’s CLI commands, run the Magento 2 installer and enable the module again.
- 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:
\Magento\Framework\Stdlib\DateTime\TimezoneInterface
\Magento\Framework\Stdlib\DateTime\Timezone
\Magento\Framework\App\ScopeResolverInterface
\Magento\Store\Model\Resolver\Website
\Magento\Store\Model\StoreManagerInterface
\Magento\Store\Model\StoreManager
\Magento\Store\Api\WebsiteRepositoryInterface
\Magento\Store\Model\WebsiteRepository
\Magento\Store\Model\ResourceModel\Website\Collection
\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”
Thank you! Saved me a lot of debug time and you offer quite the elegant solution to the issue.
You’re the man!