Undefined index: constraint_name 报错解决方法

22-12-09 16:32 237 0 技术

错误日志

[error][yii\base\ErrorException:8] yii\base\ErrorException: Undefined index: constraint_name in /path/to/vendor/yiisoft/yii2/db/mysql/Schema.php:394

原因

MySQL 8.0.21 中返回的列名大小写不一致,导致 Yii2 在获取数据库模式时出错。

解决方法

  1. 编辑 common/config/main-local.php,设置 PDO 属性 PDO::ATTR_CASE => PDO::CASE_LOWER

    'db' => [
    'class' => 'yii\db\Connection',
    'dsn' => 'mysql:host=localhost;dbname=db_name',
    'username' => 'username',
    'password' => '******',
    'attributes' => [PDO::ATTR_CASE => PDO::CASE_LOWER],
    ],
    
  2. 修改 /path/to/vendor/yiisoft/yii2/db/mysql/Schema.php:394

    foreach ($rows as $row) {
    $row = array_change_key_case($row, CASE_LOWER);
    

推荐第一种方法,使列名明确指定为一致的大小写。

参考文章:https://github.com/yiisoft/yii2/issues/18171

请登录后发表评论 点击登录

文章归档

文章日历

2024 年 09 月
01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
30 01 02 03 04 05 06

文章标签

最新评论

友情链接