Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.1] Proposal for handling primary keys in migrations. #10

Closed
Faryshta opened this issue May 26, 2016 · 5 comments
Closed

[2.1] Proposal for handling primary keys in migrations. #10

Faryshta opened this issue May 26, 2016 · 5 comments

Comments

@Faryshta
Copy link

Currently the class yii\db\Migration has a methods primaryKey() and bigPrimaryKey() which create a column based on Schema::TYPE_PK or Schema::TYPE_BIGPK respectively.

I propose to move the primaryKey() method to the yii\db\ColumnSchemaBuilder class as a column modifier with the signature.

/**
 * @var boolean|null $autoIncrement if this column use AUTO_INCREMENT.
 * this variable is null then the value will be determined on whether or not this
 * column is integer or big interger.
 * @return $this the column schema builder itself
 */
public function primaryKey($autoIncrement = null);

Usage

public function up()
{
    $this->createTable('post', [
          'id' => $this->integer()->primaryKey(), // automatically sets AUTO_INCREMENT
          //...
    ]);

    $this->createTable('tag', [
          'tag' => $this->string()->primaryKey(), // doesn't set AUTO_INCREMENT
          //...
    ]);
}

And for mixed primary keys I propose to create a new method. yii\db\Migration::tablePrimaryKey() (the name can be different) with signature.

/**
 * @var string[] $columns the columns to be used.
 * @return string
 */
public function tablePrimary(...$columns):

Usage

public function up()
{
    $this->createTable('post', [
          'id' => $this->integer()->primaryKey(),
          //...
    ]);

    $this->createTable('tag', [
          'tag' => $this->string()->primaryKey(),
          //...
    ]);

    $this->createTable('post_tag', [
          'post_id' => $this->integer()->foreignKey(),
          'tag' => $this->string()->foreignKey(),

          $this->tablePrimaryKey('post_id', 'tag');
    ])
}

the ...$column syntax is available since php5.6 but we can use simple arrays instead. If you don't like the method name tablePrimaryKey() please propose something else in comments.

@samdark
Copy link
Member

samdark commented May 26, 2016

Yes. Good idea.

@mdmunir
Copy link

mdmunir commented May 27, 2016

No break BC if only add primaryKey() method.

@Faryshta
Copy link
Author

Faryshta commented May 28, 2016

@samdark how about we folow the advice of @mdmunir ? and make Migration::primaryKey() a shortcut for Migration::integer()->primaryKey() ?

The Schema::TYPE_PK and similar constants wouldn't be useful anymore so I deleted them already on the branch where I am working for this PR.

@Faryshta
Copy link
Author

I prepared two PR's. One for 2.0 with bc compatibility as suggested by @mdmunir and one for 2.1 with BC Break.

@samdark samdark transferred this issue from yiisoft/yii2 Apr 23, 2019
@samdark samdark transferred this issue from yiisoft/db Apr 24, 2019
@Tigrov
Copy link
Member

Tigrov commented Oct 16, 2023

Duplicate of #6 and #7

@Tigrov Tigrov closed this as completed Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants