We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

ORM Caching issue(?) - table not found when moved to another database/namespace

Running 1.3.2 ALPHA 1

Having an issue where a DB table is not found after it was removed from one database and inserted put into another. "Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'intranet_alpha.report_routes' doesn't exist' It is still looking at the old database, and obviously can't find it.

I did not explicitly turn on any back-end caching -- does findFirst etc do that behind the scenes?

We are using different namespaces for the DB models. The code/namespace has been updated, in both the model and the file that is referenced in the error.

Stack trace is as follows: intranet_alpha is the DB where it used to be; reports is the new one, and you can see that is is accessing the right model, but fails when the query is attempted:

Query line is $routes_data = \Plc\Reports\ReportRoutes::findFirst($routing_condition);

Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'intranet_alpha.report_routes' doesn't exist' in /usr/local/apache2/htdocs/jh-intranet/apps/backend/controllers/VroutingController.php:24
Stack trace:
#0 [internal function]: PDO->query('DESCRIBE `repor...')
#1 [internal function]: Phalcon\Db\Adapter\Pdo->query('DESCRIBE `repor...', NULL, NULL)
#2 [internal function]: Phalcon\Db\Adapter->fetchAll('DESCRIBE `repor...', 3)
#3 [internal function]: Phalcon\Db\Adapter\Pdo\Mysql->describeColumns('report_routes', NULL)
#4 [internal function]: Phalcon\Mvc\Model\MetaData\Strategy\Introspection->getMetaData(Object(Plc\Reports\ReportRoutes), Object(Phalcon\DI\FactoryDefault))
#5 [internal function]: Phalcon\Mvc\Model\MetaData->_initialize(Object(Plc\Reports\ReportRoutes), 'plc\reports\rep...', 'report_routes', NULL)
#6 [internal function]: Phalcon\Mvc\Model\MetaData->readMetaDataIndex(Object(Plc\Reports\ReportRoutes), 1)
#7 [internal function]: Phalcon\Mvc\Model\Met in <b>/usr/local/apache2/htdocs/jh-intranet/apps/backend/controllers/VroutingController.php</b> on line 24

Any ideas what is going on? A way to flush a cache etc?

Have you updated your db service to point to the new database?

We are using multiple databases in the application.

There are other working queries that use the "new" database.

The table was moved from one database, that is still used, but no longer has the table. To the other database, where it was created as a new table.

edited Jun '14

Something else must be going on. Renamed the table, and updated the model etc.; it still can't find it.

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S02]: Base table or view not found: 1146 Table 'intranet_alpha.report_routing' doesn't exist' in /usr/local/apache2/htdocs/jh-intranet/apps/backend/controllers/VroutingController.php:25
Stack trace:
#0 [internal function]: PDO->query('DESCRIBE `repor...')#1 [internal function]: Phalcon\Db\Adapter\Pdo->query('DESCRIBE `repor...', NULL, NULL)#2 [internal function]: Phalcon\Db\Adapter->fetchAll('DESCRIBE `repor...', 3)
#3 [internal function]: Phalcon\Db\Adapter\Pdo\Mysql->describeColumns('report_routing', NULL)
#4 [internal function]: Phalcon\Mvc\Model\MetaData\Strategy\Introspection->getMetaData(Object(Plc\Reports\ReportRouting), Object(Phalcon\DI\FactoryDefault))#5 [internal function]: Phalcon\Mvc\Model\MetaData->_initialize(Object(Plc\Reports\ReportRouting), 'plc\reports\rep...', 'report_routing', NULL)#6 [internal function]: Phalcon\Mvc\Model\MetaData->readMetaData(Object(Plc\Reports\ReportRouting))
#7 [internal function]: Phalcon\Mvc\Model\MetaD in /usr/local/apache2/htdocs/jh-intranet/apps/backend/controllers/VroutingController.php on line 25
edited Jun '14

When I change the getSource method return in the model, so it doesn't match the name of the table any more, the error message changes to:

Fatal error: Uncaught exception 'Phalcon\Mvc\Model\Exception' with message 'Table "report_routinga" doesn't exist on database when dumping meta-data for Plc\Reports\ReportRouting' in /usr/local/apache2/htdocs/jh-intranet/apps/backend/controllers/VroutingController.php:25 


5.4k
Accepted
answer

The problem was that the intialize function was missing from the model, to explicitly set the database connection. It was defaulting to the default db connection.

     public function initialize()
    {
        $this->setConnectionService('reports');
    }