Вопрос

I am using migrations in Yii to create a new column with. My code works fine however, I am unsure how to set a primary key?

This is a part from my migration file:

public function up()
{
    $this->addColumn(
        'competition_prizes',
        'prize_id',
        'INT(11) UNSIGNED NOT NULL FIRST',
    );

    $this->addPrimaryKey('PK1', 'competition_prizes', 'prize_id');
}

I don't know, how to make competition_prizes column the primary key.

Это было полезно?

Решение 2

This is working now - did the following:

$this->addColumn(
    'competition_prizes',
    'prize_id',
    'INT(11) UNSIGNED NOT NULL AUTO_INCREMENT primary key FIRST'
);

Другие советы

After your addColumn function add this line

$this->addPrimaryKey('PK1', 'competition_prizes', 'prize_id')

Make sure there is no primary key column in your table.

For Yii2 version 2.0.6 - Take a look at http://www.yiiframework.com/doc-2.0/yii-db-schemabuildertrait.html#primaryKey%28%29-detail

$this->createTable('sometable', [
    'sometable_id' => $this->primaryKey(), // does not depend on Your DBMS
    'sometable_number' => 'SMALLINT(6) UNSIGNED NULL DEFAULT NULL'// will be depend on your DBMS.
]);

you can also do this

$this->addColumn('competition_prizes', 'prize_id', 'pk');

'pk' will translate to 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY' Yii2 QueryBuilder Column Type

Use constructer. At the begining of file add

use yii\db\Schema; 

And then add:

$this->addColumn(
    'competition_prizes'=> Schema::TYPE_PK,
    'prize_id'=> Schema::TYPE:INTEGER,
);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top