{"record":{"id":"8bb15dd46d553408","repo":"phalcon/cphalcon","slug":"the-index-sql-is-required-in-the-definition-arra-8bb15d","errorCode":null,"errorMessage":"The index 'sql' is required in the definition array","messagePattern":"The index 'sql' is required in the definition array","errorType":"exception","errorClass":"Phalcon\\Db\\Exceptions\\MissingDefinitionKey","httpStatus":null,"severity":"error","filePath":"phalcon/Db/Dialect/Sqlite.zep","lineNumber":294,"sourceCode":"            for check in checks {\n                let createLines[] = this->getCheckClause(check, \"`\");\n            }\n        }\n\n        let sql .= join(\",\\n\\t\", createLines) . \"\\n)\";\n\n        return sql;\n    }\n\n    /**\n     * Generates SQL to create a view\n     */\n    public function createView( string viewName,  array definition, string schemaName = null) -> string\n    {\n        var viewSql;\n\n        if unlikely !fetch viewSql, definition[\"sql\"] {\n            throw new MissingDefinitionKey(\"sql\");\n        }\n\n        return \"CREATE VIEW \" . this->prepareTable(viewName, schemaName) . \" AS \" . viewSql;\n    }\n\n    /**\n     * Generates SQL describing a table\n     *\n     * ```php\n     * print_r(\n     *     $dialect->describeColumns(\"posts\")\n     * );\n     * ```\n     */\n    public function describeColumns( string table, string schema = null) -> string\n    {\n        /**\n         * `table_xinfo` mirrors `table_info` but exposes the `hidden` column:","sourceCodeStart":276,"sourceCodeEnd":312,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Db/Dialect/Sqlite.zep#L276-L312","documentation":"Phalcon\\Db\\Dialect\\Sqlite::createView() requires the $definition array to contain a 'sql' key holding the SELECT statement for the view; otherwise MissingDefinitionKey('sql') is thrown. The dialect only wraps the supplied query in \"CREATE VIEW ... AS <sql>\", so with the key missing there is nothing to create.","triggerScenarios":"Calling createView('v_active', 'SELECT ...') and passing the query string directly instead of an array; createView('v_active', []) with an empty definition; definitions assembled from config where the 'sql' entry was lost or renamed.","commonSituations":"Copy-pasting the raw SELECT as the second argument instead of wrapping it in ['sql' => ...]; view migrations generated by tools that emit the query under a different key; empty definitions produced when a config lookup fails silently.","solutions":["Pass the query inside the array: $dialect->createView('v_active', ['sql' => 'SELECT * FROM posts WHERE active = 1'])","Validate isset($definition['sql']) && is_string($definition['sql']) && $definition['sql'] !== '' before calling createView()","Check the config/migration source that produced the definition and fix the missing view query"],"exampleFix":"// before\n$sql = $dialect->createView('v_active', 'SELECT * FROM posts WHERE active = 1');\n\n// after\n$sql = $dialect->createView('v_active', ['sql' => 'SELECT * FROM posts WHERE active = 1']);","handlingStrategy":"validation","validationCode":"if (!isset($definition['sql']) || !is_string($definition['sql']) || '' === trim($definition['sql'])) {\n    throw new InvalidArgumentException(\"createView definition requires a non-empty 'sql' string\");\n}\n$sql = $dialect->createView('v_active', $definition);","typeGuard":null,"tryCatchPattern":"try {\n    $sql = $dialect->createView('v_active', $definition);\n} catch (\\Phalcon\\Db\\Exceptions\\MissingDefinitionKey $e) {\n    throw new InvalidArgumentException('Invalid view definition for v_active: ' . $e->getMessage(), 0, $e);\n}","preventionTips":["Always wrap the SELECT in ['sql' => ...] — the second argument is an array, never a bare string","Keep view queries in one place and test that each produces a valid definition array","Fail loudly when the config lookup for a view query returns nothing"],"tags":["sqlite","phalcon-db","ddl","create-view","schema-definition"],"backgroundTag":"missing-definition-key","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}