{"record":{"id":"aab6c76428fe72e0","repo":"phalcon/cphalcon","slug":"the-index-sql-is-required-in-the-definition-arra","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":"MissingDefinitionKey","httpStatus":null,"severity":"error","filePath":"phalcon/Db/Dialect/Mysql.zep","lineNumber":352,"sourceCode":"\n        let sql .= join(\",\\n\\t\", createLines) . \"\\n)\";\n\n        if isset definition[\"options\"] {\n            let sql .= \" \" . this->getTableOptions(definition);\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        string sql, schemaClause;\n","sourceCodeStart":334,"sourceCodeEnd":370,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Db/Dialect/Mysql.zep#L334-L370","documentation":"Mysql dialect's createView() throws MissingDefinitionKey when the definition array has no 'sql' key. The view body (the SELECT statement the view wraps) is the only content a CREATE VIEW needs, so it is mandatory; schemaName remains optional.","triggerScenarios":"Calling $adapter->createView('v_robots', []) or with a definition whose key is named differently ('query', 'statement', 'definition'); passing a definition built by a helper that returns the SQL under a different key.","commonSituations":"Hand-written DDL wrappers around createView(); porting view creation code that used positional arguments; config-driven migration files where the view SQL key was renamed.","solutions":["Pass the SELECT under 'sql': $adapter->createView('v_robots', ['sql' => 'SELECT id, name FROM robots'])","Check the spelling/case of the key if the array is assembled from config or YAML/JSON sources"],"exampleFix":"// before\n$connection->createView('v_robots', ['query' => 'SELECT * FROM robots']);\n\n// after\n$connection->createView('v_robots', ['sql' => 'SELECT * FROM robots']);","handlingStrategy":"validation","validationCode":"if (!isset($definition['sql']) || trim($definition['sql']) === '') {\n    throw new InvalidArgumentException('createView requires the view body under the \"sql\" key');\n}","typeGuard":"function isCreatableViewDefinition(array $definition): bool\n{\n    return isset($definition['sql']) && is_string($definition['sql']) && trim($definition['sql']) !== '';\n}","tryCatchPattern":"try {\n    $connection->createView($name, $definition);\n} catch (\\Phalcon\\Db\\Exceptions\\MissingDefinitionKey $e) {\n    throw new RuntimeException(\"Cannot create view {$name}: \" . $e->getMessage(), 0, $e);\n}","preventionTips":["Normalize view definitions in one helper that maps any 'query' input to 'sql'","Validate config-driven migration files with a schema check"],"tags":["phalcon","mysql","ddl","create-view","definition-array"],"backgroundTag":"missing-definition-key","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}