{"record":{"id":"5f414387b09324b4","repo":"phalcon/cphalcon","slug":"the-table-must-contain-at-least-one-column-5f4143","errorCode":null,"errorMessage":"The table must contain at least one column","messagePattern":"The table must contain at least one column","errorType":"exception","errorClass":"TableMustHaveColumn","httpStatus":null,"severity":"error","filePath":"phalcon/Db/Adapter/Pdo/Postgresql.zep","lineNumber":108,"sourceCode":"\n        parent::connect(descriptor);\n\n        if !empty schema {\n            let sql = \"SET search_path TO '\" . schema . \"'\";\n\n            this->execute(sql);\n        }\n    }\n\n    /**\n     * Creates a table\n     */\n    public function createTable( string tableName,  string schemaName,  array definition) -> bool\n    {\n        var sql, queries, query, exception, columns;\n\n        if unlikely !fetch columns, definition[\"columns\"] {\n            throw new TableMustHaveColumn();\n        }\n\n        if unlikely empty columns {\n            throw new TableMustHaveColumn();\n        }\n\n        let sql = this->dialect->createTable(tableName, schemaName, definition);\n\n        let queries = explode(\";\", sql);\n\n        if count(queries) > 1 {\n            try {\n                this->{\"begin\"}();\n\n                for query in queries {\n                    if empty query {\n                        continue;\n                    }","sourceCodeStart":90,"sourceCodeEnd":126,"githubUrl":"https://github.com/phalcon/cphalcon/blob/b7419de9cd0a8a3f48441ead84c9f8415d463e25/phalcon/Db/Adapter/Pdo/Postgresql.zep#L90-L126","documentation":"Phalcon\\Db\\Adapter\\Pdo\\Postgresql::createTable() requires the $definition array to contain a 'columns' entry holding the Phalcon\\Db\\Column definitions for the new table. If the key cannot be fetched at all, the adapter throws TableMustHaveColumn before any SQL is generated. PostgreSQL cannot create a table without a column list, so the adapter rejects the definition up front.","triggerScenarios":"Calling $connection->createTable('users', 'public', $definition) where $definition has no 'columns' key - e.g. it only carries 'indexes' or 'references', the key is typo'd ('cols', 'fields', 'column', wrong case), or the array was assembled dynamically and the columns entry was never set.","commonSituations":"Migration code assembling definitions from config or model metadata and skipping the columns entry; copy-pasting a MySQL example with a different shape; key-name typos; refactors that move the column list into a variable that ends up unset.","solutions":["Add a non-empty 'columns' array whose entries are Phalcon\\Db\\Column objects (each needs at least a 'type' constant).","If the key exists under another name, rename it to exactly the lowercase string 'columns'.","When building definitions dynamically, initialize with 'columns' => [] and assert count($definition['columns']) > 0 before calling createTable()."],"exampleFix":"// before\n$connection->createTable('users', 'public', [\n    'indexes' => [['columns' => ['id'], 'type' => 'PRIMARY']],\n]); // throws TableMustHaveColumn\n\n// after\nuse Phalcon\\Db\\Column;\n\n$connection->createTable('users', 'public', [\n    'columns' => [\n        new Column('id', ['type' => Column::TYPE_INTEGER, 'primary' => true]),\n    ],\n    'indexes' => [['columns' => ['id'], 'type' => 'PRIMARY']],\n]);","handlingStrategy":"validation","validationCode":"if (!isset($definition['columns']) || !is_array($definition['columns'])) {\n    throw new InvalidArgumentException('createTable definition requires a \"columns\" array');\n}\n$connection->createTable('users', 'public', $definition);","typeGuard":"function hasCreateTableColumns(array $definition): bool\n{\n    return isset($definition['columns'])\n        && is_array($definition['columns'])\n        && $definition['columns'] !== [];\n}","tryCatchPattern":null,"preventionTips":["Start every table definition from a skeleton with 'columns' => [].","Centralize definition building in one schema factory so the key is never hand-typed.","Unit-test definition builders to always return a non-empty columns list."],"tags":["postgresql","create-table","schema-definition","phalcon-db"],"backgroundTag":"missing-required-array-key","analyzedSha":"b7419de9cd0a8a3f48441ead84c9f8415d463e25","analyzedAt":"2026-08-21T06:21:18.811Z","schemaVersion":2},"datasetVersion":"2026-08-21T11:28:35.574Z"}