src/Db/Db.php
protected function fixIndexes($tablename, array &$tableDef, $curTableDef = null) {
        // Loop through the columns and add get the primary key index.
        $primaryColumns = [];
        foreach ($tableDef['columns'] as $cname => $cdef) {
            if (val('primary', $cdef)) {
                $primaryColumns[] = $cname;
            }
        }

        // Massage the primary key index.
        $primaryFound = false;
        array_touch('indexes', $tableDef, []);
        foreach ($tableDef['indexes'] as &$indexDef) {
            array_touch('name', $indexDef, $this->buildIndexName($tablename, $indexDef));

            if (val('type', $indexDef) === Db::INDEX_PK) {
                $primaryFound = true;

                if (empty($primaryColumns)) {
                    foreach ($indexDef['columns'] as $cname) {
                        $tableDef['columns'][$cname]['primary'] = true;
                    }
                } elseif (array_diff($primaryColumns, $indexDef['columns'])) {
                    throw new \Exception("There is a mismatch in the primary key index and primary key columns.", 500);
                }
            } elseif (isset($curTableDef['indexes'])) {
                $curIndexDef = array_usearch($indexDef, $curTableDef['indexes'], [$this, 'indexCompare']);
                if ($curIndexDef && isset($curIndexDef['name'])) {
                    $indexDef['name'] = $curIndexDef['name'];
                }
            }
        }

        if (!$primaryFound && !empty($primaryColumns)) {
            $tableDef['indexes'][db::INDEX_PK] = [
                'columns' => $primaryColumns,
                'type' => Db::INDEX_PK
            ];
        }
    }
protected function fixIndexes($tablename, array &$tableDef, $curTableDef = null) {
        // Loop through the columns and add get the primary key index.
        $primaryColumns = [];
        foreach ($tableDef['columns'] as $cname => $cdef) {
            if (val('primary', $cdef)) {
                $primaryColumns[] = $cname;
            }
        }

        // Massage the primary key index.
        $primaryFound = false;
        array_touch('indexes', $tableDef, []);
        foreach ($tableDef['indexes'] as &$indexDef) {
            array_touch('name', $indexDef, $this->buildIndexName($tablename, $indexDef));

            if (val('type', $indexDef) === Db::INDEX_PK) {
                $primaryFound = true;

                if (empty($primaryColumns)) {
                    foreach ($indexDef['columns'] as $cname) {
                        $tableDef['columns'][$cname]['primary'] = true;
                    }
                } elseif (array_diff($primaryColumns, $indexDef['columns'])) {
                    throw new \Exception("There is a mismatch in the primary key index and primary key columns.", 500);
                }
            } elseif (isset($curTableDef['indexes'])) {
                $curIndexDef = array_usearch($indexDef, $curTableDef['indexes'], [$this, 'indexCompare']);
                if ($curIndexDef && isset($curIndexDef['name'])) {
                    $indexDef['name'] = $curIndexDef['name'];
                }
            }
        }

        if (!$primaryFound && !empty($primaryColumns)) {
            $tableDef['indexes'][db::INDEX_PK] = [
                'columns' => $primaryColumns,
                'type' => Db::INDEX_PK
            ];
        }
    }
Move the primary key index into the correct place for database drivers.

@param string $tablename The name of the table.
@param array &$tableDef The table definition.
@param array|null $curTableDef The current database table def used to resolve conflicts in some names.
@throws \Exception Throws an exception when there is a mismatch between the primary index and the primary key
defined on the columns themselves.