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


    1.修改数据库连接信息

    文件位置 config/db.php

    添加默认Schema

    1. return [
    2. 'class' => 'yii\db\Connection',
    3. 'dsn' => 'pgsql:host=127.0.0.1;port=5432;dbname=postgres',
    4. 'username' => 'postgres',
    5. 'password' => 'Pass@123',
    6. 'charset' => 'utf8',
    7. //'enableSchemaCache' => true,//表结构是否缓存
    8. //'schemaCacheDuration' => 86400,
    9. //'schemaCache' => 'cache',
    10. 'schemaMap' => [
    11. 'pgsql'=> [
    12. 'class'=>'yii\db\pgsql\Schema',
    13. 'defaultSchema' => 'postgres' //默认Schema
    14. ]
    15. ]
    16. ];

    2.修改pgsql的Schema函数

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

    修改函数 findColumns($table)

    1. protected function findColumns($table)
    2. {
    3. $tableName = $this->db->quoteValue($table->name);
    4. $schemaName = $this->db->quoteValue($table->schemaName);
    5. $sql = "SELECT * FROM information_schema.columns WHERE table_schema='". $table->schemaName ."' and table_name = '". $table->name ."'";
    6. $columns = $this->db->createCommand($sql)->queryAll();
    7. if (empty($columns)) {
    8. return false;
    9. }
    10. $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
    11. FROM information_schema.table_constraints tc
    12. JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name
    13. JOIN information_schema.constraint_column_usage ccu ON ccu.constraint_name = tc.constraint_name
    14. WHERE tc.constraint_schema='". $table->schemaName ."' and constraint_type = 'PRIMARY KEY' AND tc.table_name='". $table->name ."'";
    15. $prkcolumns = $this->db->createCommand($prksql)->queryAll();
    16. foreach ($columns as $column) {
    17. if ($this->db->slavePdo->getAttribute(\PDO::ATTR_CASE) === \PDO::CASE_UPPER) {
    18. $column = array_change_key_case($column, CASE_LOWER);
    19. }
    20. if(stripos($column["column_default"], "nextval")!==false && stripos($column["column_default"], "_seq")!==false){
    21. $column["is_autoinc"] = 1;
    22. }else{
    23. $column["is_autoinc"] = 0;
    24. }
    25. $column["size"] = $column["character_maximum_length"];
    26. $column["is_pkey"] = 0;
    27. foreach($prkcolumns as $oneprkcol){
    28. if($oneprkcol["column_name"]==$column["column_name"]){
    29. $column["is_pkey"] = 1;
    30. }
    31. }
    32. $column = $this->loadColumnSchema($column);
    33. $table->columns[$column->name] = $column;
    34. if ($column->isPrimaryKey) {
    35. $table->primaryKey[] = $column->name;
    36. if ($table->sequenceName === null && preg_match("/nextval\\('\"?\\w+\"?\.?\"?\\w+\"?'(::regclass)?\\)/", $column->defaultValue) === 1) {
    37. $table->sequenceName = preg_replace(['/nextval/', '/::/', '/regclass/', '/\'\)/', '/\(\'/'], '', $column->defaultValue);
    38. }
    39. $column->defaultValue = null;
    40. } elseif ($column->defaultValue) {
    41. if ($column->type === 'timestamp' && $column->defaultValue === 'now()') {
    42. $column->defaultValue = new Expression($column->defaultValue);
    43. } elseif ($column->type === 'boolean') {
    44. $column->defaultValue = ($column->defaultValue === 'true');
    45. } elseif (strncasecmp($column->dbType, 'bit', 3) === 0 || strncasecmp($column->dbType, 'varbit', 6) === 0) {
    46. $column->defaultValue = bindec(trim($column->defaultValue, 'B\''));
    47. } elseif (preg_match("/^'(.*?)'::/", $column->defaultValue, $matches)) {
    48. $column->defaultValue = $column->phpTypecast($matches[1]);
    49. } elseif (preg_match('/^(\()?(.*?)(?(1)\))(?:::.+)?$/', $column->defaultValue, $matches)) {
    50. if ($matches[2] === 'NULL') {
    51. $column->defaultValue = null;
    52. } else {
    53. $column->defaultValue = $column->phpTypecast($matches[2]);
    54. }
    55. } else {
    56. $column->defaultValue = $column->phpTypecast($column->defaultValue);
    57. }
    58. }
    59. }
    60. return true;
    61. }

  • 相关阅读:
    java家庭理财收支管理系统
    Postman传参后台接收问题
    TypeScrip 接口和对象类型 数组类型 函数
    《TCPIP网络编程》课后练习答案第一部分1~5章 尹圣雨
    患上肝内胆管结石症状有哪些?
    ABAP datum_range中的四个组件的作用
    ArcGIS基础:不同方法修改栅格数据像元值
    玩转字符串——不一样的风格
    如何改善交通管制带来的交通拥堵?
    推荐一个对pytorch代码详细注释的github项目
  • 原文地址:https://blog.csdn.net/yutiedun/article/details/134541020