phalcon/cphalcon · error · MissingDefinitionKey

The index 'sql' is required in the definition array

Error message

The index 'sql' is required in the definition array

What it means

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.

Source

Thrown at phalcon/Db/Dialect/Mysql.zep:352

        let sql .= join(",\n\t", createLines) . "\n)";

        if isset definition["options"] {
            let sql .= " " . this->getTableOptions(definition);
        }

        return sql;
    }

    /**
     * Generates SQL to create a view
     */
    public function createView( string viewName,  array definition, string schemaName = null) -> string
    {
        var viewSql;

        if unlikely !fetch viewSql, definition["sql"] {
            throw new MissingDefinitionKey("sql");
        }

        return "CREATE VIEW " . this->prepareTable(viewName, schemaName) . " AS " . viewSql;
    }

    /**
     * Generates SQL describing a table
     *
     * ```php
     * print_r(
     *     $dialect->describeColumns("posts")
     * );
     * ```
     */
    public function describeColumns( string table, string schema = null) -> string
    {
        string sql, schemaClause;

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Pass the SELECT under 'sql': $adapter->createView('v_robots', ['sql' => 'SELECT id, name FROM robots'])
  2. Check the spelling/case of the key if the array is assembled from config or YAML/JSON sources

Example fix

// before
$connection->createView('v_robots', ['query' => 'SELECT * FROM robots']);

// after
$connection->createView('v_robots', ['sql' => 'SELECT * FROM robots']);
Defensive patterns

Strategy: validation

Validate before calling

if (!isset($definition['sql']) || trim($definition['sql']) === '') {
    throw new InvalidArgumentException('createView requires the view body under the "sql" key');
}

Type guard

function isCreatableViewDefinition(array $definition): bool
{
    return isset($definition['sql']) && is_string($definition['sql']) && trim($definition['sql']) !== '';
}

Try / catch

try {
    $connection->createView($name, $definition);
} catch (\Phalcon\Db\Exceptions\MissingDefinitionKey $e) {
    throw new RuntimeException("Cannot create view {$name}: " . $e->getMessage(), 0, $e);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/aab6c76428fe72e0. Report an issue: GitHub.