1. In Yii 2.0, create a primary key column using a database migration. By default, it uses $this->primaryKey(). The created primary key column defaults to the int(11) type. See Figure 1.

2. A colleague plans to create a primary key column of type varchar(32). See Figure 2.

3. The primaryKey() method only accepts a single column size parameter. Therefore, a string-type primary key cannot be created using this method.
PHP
/**
* Creates a primary key column.
* @param int $length column size or precision definition.
* This parameter will be ignored if not supported by the DBMS.
* @return ColumnSchemaBuilder the column instance which can be further customized.
* @since 2.0.6
*/
public function primaryKey($length = null)
{
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_PK, $length);
}
4. Finally, I decided to first create a string-type field, and after the table is created, set the field as the primary key (using the addPrimaryKey() method). This creates a primary key on an existing field.
PHP
/**
* Builds and executes a SQL statement for creating a primary key.
* The method will properly quote the table and column names.
* @param string $name the name of the primary key constraint.
* @param string $table the table that the primary key constraint will be added to.
* @param string|array $columns comma separated string or array of columns that the primary key will consist of.
*/
public function addPrimaryKey($name, $table, $columns)
{
$time = $this->beginCommand("add primary key $name on $table (" . (is_array($columns) ? implode(',', $columns) : $columns) . ')');
$this->db->createCommand()->addPrimaryKey($name, $table, $columns)->execute();
$this->endCommand($time);
}
5. The final code implementation is as follows. See Figure 3.

PHP
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
$tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB COMMENT="渠道"';
};
$this->createTable('{{%ccp_platform_account}}', [
'account_uuid' => $this->string(32)->notNull(),
], $tableOptions);
$this->addPrimaryKey('account_uuid','{{%ccp_platform_account}}','account_uuid');
