Yii2 ActiveRecord连接OpenGauss提示表不存在table not exist

2023-11-21 22:12

本文主要是介绍Yii2 ActiveRecord连接OpenGauss提示表不存在table not exist,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.修改数据库连接信息

文件位置 config/db.php

添加默认Schema

return ['class' => 'yii\db\Connection','dsn' => 'pgsql:host=127.0.0.1;port=5432;dbname=postgres','username' => 'postgres','password' => 'Pass@123','charset' => 'utf8',//'enableSchemaCache' => true,//表结构是否缓存//'schemaCacheDuration' => 86400,//'schemaCache' => 'cache','schemaMap' => ['pgsql'=> ['class'=>'yii\db\pgsql\Schema','defaultSchema' => 'postgres' //默认Schema]]
];

2.修改pgsql的Schema函数

文件位置 vendor/yiisoft/yii2/db/pgsql/Schema.php

修改函数 findColumns($table)

protected function findColumns($table)
{$tableName = $this->db->quoteValue($table->name);$schemaName = $this->db->quoteValue($table->schemaName);$sql = "SELECT * FROM information_schema.columns WHERE table_schema='". $table->schemaName ."' and table_name = '". $table->name ."'";$columns = $this->db->createCommand($sql)->queryAll();if (empty($columns)) {return false;}$prksql = "SELECT tc.constraint_name, tc.table_name, kcu.column_name, ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name FROM information_schema.table_constraints tc JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name JOIN information_schema.constraint_column_usage ccu ON ccu.constraint_name = tc.constraint_name WHERE tc.constraint_schema='". $table->schemaName ."' and constraint_type = 'PRIMARY KEY' AND tc.table_name='". $table->name ."'";$prkcolumns = $this->db->createCommand($prksql)->queryAll();foreach ($columns as $column) {if ($this->db->slavePdo->getAttribute(\PDO::ATTR_CASE) === \PDO::CASE_UPPER) {$column = array_change_key_case($column, CASE_LOWER);}if(stripos($column["column_default"], "nextval")!==false && stripos($column["column_default"], "_seq")!==false){$column["is_autoinc"] = 1;}else{$column["is_autoinc"] = 0;}$column["size"] = $column["character_maximum_length"];$column["is_pkey"] = 0;foreach($prkcolumns as $oneprkcol){if($oneprkcol["column_name"]==$column["column_name"]){$column["is_pkey"] = 1;}}$column = $this->loadColumnSchema($column);$table->columns[$column->name] = $column;if ($column->isPrimaryKey) {$table->primaryKey[] = $column->name;if ($table->sequenceName === null && preg_match("/nextval\\('\"?\\w+\"?\.?\"?\\w+\"?'(::regclass)?\\)/", $column->defaultValue) === 1) {$table->sequenceName = preg_replace(['/nextval/', '/::/', '/regclass/', '/\'\)/', '/\(\'/'], '', $column->defaultValue);}$column->defaultValue = null;} elseif ($column->defaultValue) {if ($column->type === 'timestamp' && $column->defaultValue === 'now()') {$column->defaultValue = new Expression($column->defaultValue);} elseif ($column->type === 'boolean') {$column->defaultValue = ($column->defaultValue === 'true');} elseif (strncasecmp($column->dbType, 'bit', 3) === 0 || strncasecmp($column->dbType, 'varbit', 6) === 0) {$column->defaultValue = bindec(trim($column->defaultValue, 'B\''));} elseif (preg_match("/^'(.*?)'::/", $column->defaultValue, $matches)) {$column->defaultValue = $column->phpTypecast($matches[1]);} elseif (preg_match('/^(\()?(.*?)(?(1)\))(?:::.+)?$/', $column->defaultValue, $matches)) {if ($matches[2] === 'NULL') {$column->defaultValue = null;} else {$column->defaultValue = $column->phpTypecast($matches[2]);}} else {$column->defaultValue = $column->phpTypecast($column->defaultValue);}}}return true;
}

这篇关于Yii2 ActiveRecord连接OpenGauss提示表不存在table not exist的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/405453

相关文章

pycharm远程连接服务器运行pytorch的过程详解

《pycharm远程连接服务器运行pytorch的过程详解》:本文主要介绍在Linux环境下使用Anaconda管理不同版本的Python环境,并通过PyCharm远程连接服务器来运行PyTorc... 目录linux部署pytorch背景介绍Anaconda安装Linux安装pytorch虚拟环境安装cu

Nginx设置连接超时并进行测试的方法步骤

《Nginx设置连接超时并进行测试的方法步骤》在高并发场景下,如果客户端与服务器的连接长时间未响应,会占用大量的系统资源,影响其他正常请求的处理效率,为了解决这个问题,可以通过设置Nginx的连接... 目录设置连接超时目的操作步骤测试连接超时测试方法:总结:设置连接超时目的设置客户端与服务器之间的连接

SQL 中多表查询的常见连接方式详解

《SQL中多表查询的常见连接方式详解》本文介绍SQL中多表查询的常见连接方式,包括内连接(INNERJOIN)、左连接(LEFTJOIN)、右连接(RIGHTJOIN)、全外连接(FULLOUTER... 目录一、连接类型图表(ASCII 形式)二、前置代码(创建示例表)三、连接方式代码示例1. 内连接(I

java如何通过Kerberos认证方式连接hive

《java如何通过Kerberos认证方式连接hive》该文主要介绍了如何在数据源管理功能中适配不同数据源(如MySQL、PostgreSQL和Hive),特别是如何在SpringBoot3框架下通过... 目录Java实现Kerberos认证主要方法依赖示例续期连接hive遇到的问题分析解决方式扩展思考总

Python中连接不同数据库的方法总结

《Python中连接不同数据库的方法总结》在数据驱动的现代应用开发中,Python凭借其丰富的库和强大的生态系统,成为连接各种数据库的理想编程语言,下面我们就来看看如何使用Python实现连接常用的几... 目录一、连接mysql数据库二、连接PostgreSQL数据库三、连接SQLite数据库四、连接Mo

oracle如何连接登陆SYS账号

《oracle如何连接登陆SYS账号》在Navicat12中连接Oracle11g的SYS用户时,如果设置了新密码但连接失败,可能是因为需要以SYSDBA或SYSOPER角色连接,解决方法是确保在连接... 目录oracle连接登陆NmOtMSYS账号工具问题解决SYS用户总结oracle连接登陆SYS账号

VScode连接远程Linux服务器环境配置图文教程

《VScode连接远程Linux服务器环境配置图文教程》:本文主要介绍如何安装和配置VSCode,包括安装步骤、环境配置(如汉化包、远程SSH连接)、语言包安装(如C/C++插件)等,文中给出了详... 目录一、安装vscode二、环境配置1.中文汉化包2.安装remote-ssh,用于远程连接2.1安装2

关于rpc长连接与短连接的思考记录

《关于rpc长连接与短连接的思考记录》文章总结了RPC项目中长连接和短连接的处理方式,包括RPC和HTTP的长连接与短连接的区别、TCP的保活机制、客户端与服务器的连接模式及其利弊分析,文章强调了在实... 目录rpc项目中的长连接与短连接的思考什么是rpc项目中的长连接和短连接与tcp和http的长连接短

Xshell远程连接失败以及解决方案

《Xshell远程连接失败以及解决方案》本文介绍了在Windows11家庭版和CentOS系统中解决Xshell无法连接远程服务器问题的步骤,在Windows11家庭版中,需要通过设置添加SSH功能并... 目录一.问题描述二.原因分析及解决办法2.1添加ssh功能2.2 在Windows中开启ssh服务2

Mybatis提示Tag name expected的问题及解决

《Mybatis提示Tagnameexpected的问题及解决》MyBatis是一个开源的Java持久层框架,用于将Java对象与数据库表进行映射,它提供了一种简单、灵活的方式来访问数据库,同时也... 目录概念说明MyBATis特点发现问题解决问题第一种方式第二种方式问题总结概念说明MyBatis(原名