简体   繁体   English

Yii2 中的数据库迁移问题

[英]Issues with Database Migrations in Yii2

I am new to Database Migrations and I am trying to figure out exactly how they work.我是数据库迁移的新手,我正试图弄清楚它们是如何工作的。 I am working in Yii2.我在 Yii2 工作。 So if I go into my local database and create a new column called test and then run console/yii migration/update job因此,如果我进入本地数据库并创建一个名为test的新列,然后运行console/yii migration/update job

First Migration File:第一个迁移文件:

<?php

use yii\db\Migration;

<?php

use yii\db\Migration;

class m211012_201641_01_create_table_job extends Migration
{
    public function up()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB';
        }

        $this->createTable(
            '{{%job}}',
            [
                'job_id' => $this->primaryKey(),
                'client_id' => $this->integer()->notNull(),
                'title' => $this->string(),
                'instruction' => $this->string(),
                'recurring_job' => $this->string(),
                'start_date' => $this->date(),
                'end_date' => $this->date(),
                'start_time' => $this->time(),
                'end_time' => $this->time(),
                'repeat' => $this->string(),
                'duration' => $this->string()->notNull(),
                'first_visit' => $this->date(),
                'last_visit' => $this->date(),
                'total_vist' => $this->string()->notNull(),
                'test' => $this->integer(),
            ],
            $tableOptions
        );

        $this->addForeignKey(
            'job_fk0',
            '{{%job}}',
            ['client_id'],
            '{{%client}}',
            ['client_id'],
            'RESTRICT',
            'RESTRICT'
        );
    }

    public function down()
    {
        $this->dropTable('{{%job}}');
    }
}


Updated Migration File更新的迁移文件

<?php

use yii\db\Migration;

class m211012_201641_01_create_table_job extends Migration
{
    public function up()
    {
        $tableOptions = null;
        if ($this->db->driverName === 'mysql') {
            $tableOptions = 'CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB';
        }

        $this->createTable(
            '{{%job}}',
            [
                'job_id' => $this->primaryKey(),
                'client_id' => $this->integer()->notNull(),
                'title' => $this->string(),
                'instruction' => $this->string(),
                'recurring_job' => $this->string(),
                'start_date' => $this->date(),
                'end_date' => $this->date(),
                'start_time' => $this->time(),
                'end_time' => $this->time(),
                'repeat' => $this->string(),
                'duration' => $this->string()->notNull(),
                'first_visit' => $this->date(),
                'last_visit' => $this->date(),
                'total_vist' => $this->string()->notNull(),
                'test' => $this->integer(),
            ],
            $tableOptions
        );

        $this->addForeignKey(
            'job_fk0',
            '{{%job}}',
            ['client_id'],
            '{{%client}}',
            ['client_id'],
            'RESTRICT',
            'RESTRICT'
        );
    }

    public function down()
    {
        $this->dropTable('{{%job}}');
    }
}

The issue that I am running into is that If i have my coworker run this Migration.我遇到的问题是,如果我让我的同事运行此迁移。

It will throw an error stating:它会抛出一个错误说明:

# console/yii migrate
Yii Migration Tool (based on Yii v2.0.43)

Total 1 new migration to be applied:
    m211012_201641_01_create_table_job

Apply the above migration? (yes|no) [no]:yes
*** applying m211012_201641_01_create_table_job
    > create table {{%job}} ...Exception 'yii\db\Exception' with message 'SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'job' already exists
The SQL being executed was: CREATE TABLE `job` (
    `job_id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    `client_id` int(11) NOT NULL,
    `title` varchar(255),
    `instruction` varchar(255),
    `recurring_job` varchar(255),
    `start_date` date,
    `end_date` date,
    `start_time` time,
    `end_time` time,
    `repeat` varchar(255),
    `duration` varchar(255) NOT NULL,
    `first_visit` date,
    `last_visit` date,
    `total_vist` varchar(255) NOT NULL,
    `test` int(11)
) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE=InnoDB'

in /var/www/html/online/vendor/yiisoft/yii2/db/Schema.php:678

Error Info:
Array
(
    [0] => 42S01
    [1] => 1050
    [2] => Table 'job' already exists
)

Caused by: Exception 'PDOException' with message 'SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'job' already exists'

What exactly am I doing wrong?我到底做错了什么? Am I not understanding Database Migrations correctly?我没有正确理解数据库迁移吗?

For adding a column to the table you should create new migration.要向表中添加一列,您应该创建新的迁移。

this is the example provided in yii2 guide这是 yii2 指南中提供的示例

class m150811_220037_add_position_column_to_post_table extends Migration
 {
    public function up()
    {
        $this->addColumn('post', 'position', $this->integer());
    }

    public function down()
   {
       $this->dropColumn('post', 'position');
   }
}

It looks that you are using my package in here - you must state it in your question because it is not obvious to everybody and because of that - confusing.看起来你在这里使用我的- 你必须在你的问题中说明它,因为它对每个人都不是显而易见的,因此 - 令人困惑。

Looks like you have that table already created in the database but there is no migration of it applied and registered so when you are trying to update it with new column it generates "create" migration instead of "update" one.看起来您已经在数据库中创建了该表,但是没有应用和注册它的迁移,因此当您尝试使用新列更新它时,它会生成“创建”迁移而不是“更新”迁移。

You need to apply "create" migration first either by generating it with fh option ( fixHistory ) or by adding it some other way to migration_history table.您需要首先应用“创建”迁移,方法是使用fh选项( fixHistory )生成它,或者将其以其他方式添加到migration_history表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM