{"record":{"id":"5f6debccb90ac4d5","repo":"yiisoft/yii2","slug":"the-table-does-not-exist-table","errorCode":null,"errorMessage":"The table does not exist: {table}","messagePattern":"The table does not exist: (.+?)","errorType":"exception","errorClass":"yii\\base\\InvalidConfigException","httpStatus":null,"severity":"critical","filePath":"framework/db/ActiveRecord.php","lineNumber":438,"sourceCode":"     */\n    public static function tableName()\n    {\n        return '{{%' . Inflector::camel2id(StringHelper::basename(get_called_class()), '_') . '}}';\n    }\n\n    /**\n     * Returns the schema information of the DB table associated with this AR class.\n     * @return TableSchema the schema information of the DB table associated with this AR class.\n     * @throws InvalidConfigException if the table for the AR class does not exist.\n     */\n    public static function getTableSchema()\n    {\n        $tableSchema = static::getDb()\n            ->getSchema()\n            ->getTableSchema(static::tableName());\n\n        if ($tableSchema === null) {\n            throw new InvalidConfigException('The table does not exist: ' . static::tableName());\n        }\n\n        return $tableSchema;\n    }\n\n    /**\n     * Returns the primary key name(s) for this AR class.\n     * The default implementation will return the primary key(s) as declared\n     * in the DB table that is associated with this AR class.\n     *\n     * If the DB table does not declare any primary key, you should override\n     * this method to return the attributes that you want to use as primary keys\n     * for this AR class.\n     *\n     * Note that an array should be returned even for a table with single primary key.\n     *\n     * @return string[] the primary keys of the associated database table.\n     */","sourceCodeStart":420,"sourceCodeEnd":456,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/ActiveRecord.php#L420-L456","documentation":"ActiveRecord::getTableSchema() asks the DB connection's schema reader for the schema of tableName(); when the reader returns null the connection cannot see any such table, and InvalidConfigException is thrown. The call happens implicitly on first AR use (attribute detection, find, insert), so any model mapped to a missing table fails on its first query.","triggerScenarios":"Using an AR class whose table was never created (migrations not run); tableName() has a typo or wrong {{%...}} table prefix; the model's getDb() points at a different database than the one holding the table; table name casing differs from the DDL on case-sensitive filesystems (Linux).","commonSituations":"Fresh environment where migrations were skipped; moving between environments with different tablePrefix config; stale schema cache after the table was created or renamed (enableSchemaCache on); selecting the wrong 'db' component in a multi-database app.","solutions":["Run the pending migrations, or verify the table exists with the same connection the model uses","Check tableName() spelling and the Connection::tablePrefix value for {{%...}} names","Confirm the model's getDb() returns the connection that actually holds the table","Flush the schema cache after DDL changes (e.g. Yii::$app->cache->flush() or disable enableSchemaCache while developing)"],"exampleFix":"// before\nclass Payment extends ActiveRecord\n{\n    public static function tableName() { return 'payements'; } // typo\n}\n\n// after\nclass Payment extends ActiveRecord\n{\n    public static function tableName() { return '{{%payments}}'; }\n}","handlingStrategy":"validation","validationCode":"if (Model::getDb()->getSchema()->getTableSchema(Model::tableName()) === null) {\n    throw new InvalidConfigException('Table ' . Model::tableName() . ' missing on this connection. Run migrations.');\n}","typeGuard":null,"tryCatchPattern":"try {\n    $rows = Model::find()->all();\n} catch (yii\\base\\InvalidConfigException $e) {\n    if (strpos($e->getMessage(), 'The table does not exist') === 0) {\n        // surface an actionable setup error\n        throw new RuntimeException('Database not migrated: ' . $e->getMessage(), 0, $e);\n    }\n    throw $e;\n}","preventionTips":["Run a schema check in deployment smoke tests for every AR class","Keep tableName() names in one place and cover them with integration tests against a migrated schema","Flush schema cache in release scripts after migrations"],"tags":["database","schema","migration","table","configuration","schema-cache"],"backgroundTag":"table-not-found","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}