{"record":{"id":"61731b72de1a24dc","repo":"yiisoft/yii2","slug":"postgresql-does-not-support-default-value-constrai","errorCode":null,"errorMessage":"PostgreSQL does not support default value constraints.","messagePattern":"PostgreSQL does not support default value constraints\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"framework/db/pgsql/Schema.php","lineNumber":287,"sourceCode":"    {\n        return $this->loadTableConstraints($tableName, 'uniques');\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    protected function loadTableChecks($tableName)\n    {\n        return $this->loadTableConstraints($tableName, 'checks');\n    }\n\n    /**\n     * {@inheritdoc}\n     * @throws NotSupportedException if this method is called.\n     */\n    protected function loadTableDefaultValues($tableName)\n    {\n        throw new NotSupportedException('PostgreSQL does not support default value constraints.');\n    }\n\n    /**\n     * Creates a query builder for the PostgreSQL database.\n     * @return QueryBuilder query builder instance\n     */\n    public function createQueryBuilder()\n    {\n        return Yii::createObject(QueryBuilder::className(), [$this->db]);\n    }\n\n    /**\n     * Resolves the table name and schema name (if any).\n     * @param TableSchema $table the table metadata object\n     * @param string $name the table name\n     */\n    protected function resolveTableNames($table, $name)\n    {","sourceCodeStart":269,"sourceCodeEnd":305,"githubUrl":"https://github.com/yiisoft/yii2/blob/66f00d18a29b520f85e8e8f1e32d1e7e7b556cac/framework/db/pgsql/Schema.php#L269-L305","documentation":"yii\\db\\pgsql\\Schema::loadTableDefaultValues() implements the base Schema's named-DEFAULT-constraint contract, but PostgreSQL, like MySQL, expresses defaults in column DDL rather than as separate constraint objects. The method therefore unconditionally throws NotSupportedException.","triggerScenarios":"Calling $db->getTableDefaultValues('tbl') on a PostgreSQL connection, usually from generic code that enumerates all constraint accessors without driver awareness.","commonSituations":"Schema tooling or admin generators written against mssql (the driver that does support named defaults) reused on PostgreSQL; migration diff engines probing every constraint type.","solutions":["Do not call getTableDefaultValues() on PostgreSQL.","Read per-column defaults from getTableSchema($t)->columns[$name]->defaultValue.","Branch on driver name or catch NotSupportedException in generic introspection loops."],"exampleFix":"// before\n$defaults = $db->getTableDefaultValues('invoice');\n\n// after\n$defaults = array_map(\n    fn($c) => $c->defaultValue,\n    $db->getTableSchema('invoice', true)->columns\n);","handlingStrategy":"try-catch","validationCode":"if ($db->driverName === 'pgsql') {\n    $defaults = array_map(fn($c) => $c->defaultValue, $db->getTableSchema($t, true)->columns);\n} else {\n    $defaults = $db->getTableDefaultValues($t);\n}","typeGuard":null,"tryCatchPattern":"try {\n    $defaults = $db->getTableDefaultValues($table);\n} catch (\\yii\\db\\NotSupportedException $e) {\n    $defaults = []; // pgsql has no named default constraints\n}","preventionTips":["Use column-schema defaults for PostgreSQL.","Maintain a per-driver capability table in schema tooling.","Treat NotSupportedException from getTable* accessors as an empty result, not an error."],"tags":["yii2","php","postgresql","pgsql","database","default-constraint","not-supported"],"backgroundTag":"unsupported-driver-operation","analyzedSha":"66f00d18a29b520f85e8e8f1e32d1e7e7b556cac","analyzedAt":"2026-08-17T05:17:23.470Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}