src/models/Schema.php
public function getKey(TableSchema $table, $indent = 0)
    {
        $definition = '';
        $textIndent = $this->textIndent($indent);
        $tablePrefix = $this->db->tablePrefix;
        $tableName = static::removePrefix($table->name, $tablePrefix);

        $data = $this->db->createCommand('SHOW KEYS FROM ' . $table->name)->queryAll();
        if (empty($data)) {
            return '';
        }

        // 获取索引数据,包括主键
        $keyData = [];
        foreach ($data as $value) {
            $keyName = static::removePrefix($value['Key_name'], $tablePrefix);
            if (empty($keyData[$keyName])) {
                $keyData[$keyName] = [];
            }

            array_push($keyData[$keyName], [
                'table' => $value['Table'],
                'is_unique' => $value['Non_unique'] ? 0 : 1,
                'column_name' => $value['Column_name'],
                'allow_null' => $value['Null'],
                'comment' => $value['Comment'],
                'index_comment' => $value['Index_comment'],
            ]);
        }

        foreach ($keyData as $keyName => $value) {
            $columns = implode(',', array_column($value,'column_name'));
            $definition .= $textIndent;
            $definition .= "\$this->runSuccess['$keyName'] = "; // record which key add successfully

            // primary key
            if ('PRIMARY' === $keyName) {
                $definition .= "\$this->addPrimaryKey(null, '{{%$tableName}}', '$columns');" . self::ENTER;

                foreach ($keyData['PRIMARY'] as $column) {
                    $column = $column['column_name']; // table column name
                    // auto_increment
                    if ($table->columns[$column]->autoIncrement) {
                        $columnType = $table->columns[$column]->type;
                        $property = $table->columns[$column]->unsigned ? 'unsigned' : '';
                        $auto_increment = $this->getAutoIncrementNumber($table->name);
                        $definition .= $textIndent;
                        $definition .= "\$this->runSuccess['addAutoIncrement'] = ";
                        $definition .= "\$this->addAutoIncrement('{{%$tableName}}', '$column', '$columnType', '$property', $auto_increment);" . self::ENTER;
                    }
                }

            } else {
                // other keys except primary key
                $isUnique = $value[0]['is_unique'];
                $definition .= "\$this->createIndex('$keyName', '{{%$tableName}}', '$columns', {$isUnique});" . self::ENTER;
            }


        }

        return $definition;
    }
public function getKey(TableSchema $table, $indent = 0)
    {
        $definition = '';
        $textIndent = $this->textIndent($indent);
        $tablePrefix = $this->db->tablePrefix;
        $tableName = static::removePrefix($table->name, $tablePrefix);

        $data = $this->db->createCommand('SHOW KEYS FROM ' . $table->name)->queryAll();
        if (empty($data)) {
            return '';
        }

        // 获取索引数据,包括主键
        $keyData = [];
        foreach ($data as $value) {
            $keyName = static::removePrefix($value['Key_name'], $tablePrefix);
            if (empty($keyData[$keyName])) {
                $keyData[$keyName] = [];
            }

            array_push($keyData[$keyName], [
                'table' => $value['Table'],
                'is_unique' => $value['Non_unique'] ? 0 : 1,
                'column_name' => $value['Column_name'],
                'allow_null' => $value['Null'],
                'comment' => $value['Comment'],
                'index_comment' => $value['Index_comment'],
            ]);
        }

        foreach ($keyData as $keyName => $value) {
            $columns = implode(',', array_column($value,'column_name'));
            $definition .= $textIndent;
            $definition .= "\$this->runSuccess['$keyName'] = "; // record which key add successfully

            // primary key
            if ('PRIMARY' === $keyName) {
                $definition .= "\$this->addPrimaryKey(null, '{{%$tableName}}', '$columns');" . self::ENTER;

                foreach ($keyData['PRIMARY'] as $column) {
                    $column = $column['column_name']; // table column name
                    // auto_increment
                    if ($table->columns[$column]->autoIncrement) {
                        $columnType = $table->columns[$column]->type;
                        $property = $table->columns[$column]->unsigned ? 'unsigned' : '';
                        $auto_increment = $this->getAutoIncrementNumber($table->name);
                        $definition .= $textIndent;
                        $definition .= "\$this->runSuccess['addAutoIncrement'] = ";
                        $definition .= "\$this->addAutoIncrement('{{%$tableName}}', '$column', '$columnType', '$property', $auto_increment);" . self::ENTER;
                    }
                }

            } else {
                // other keys except primary key
                $isUnique = $value[0]['is_unique'];
                $definition .= "\$this->createIndex('$keyName', '{{%$tableName}}', '$columns', {$isUnique});" . self::ENTER;
            }


        }

        return $definition;
    }
get column key
@param TableSchema $table
@param $indent int text-indent 文本缩进
@return string