【问题标题】:Issues with Database Migrations in Yii2Yii2 中的数据库迁移问题
【发布时间】:2021-12-01 10:27:55
【问题描述】:

我是数据库迁移的新手,我正试图弄清楚它们是如何工作的。我在 Yii2 工作。因此,如果我进入本地数据库并创建一个名为 test 的新列,然后运行 ​​console/yii migration/update job

第一个迁移文件:

<?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}}');
    }
}


更新的迁移文件

<?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}}');
    }
}

我遇到的问题是,如果我让我的同事运行此迁移。

它会抛出一个错误说明:

# 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'

我到底做错了什么?我没有正确理解数据库迁移吗?

【问题讨论】:

  • mysql 不允许在同一个数据库下创建重复的表名。与迁移无关。
  • 在 Yii 迁移中没有像 update 这样的命令。阅读文档以更好地理解

标签: php mysql yii2 database-migration


【解决方案1】:

看起来你在这里使用了我的package - 你必须在你的问题中说明它,因为这对每个人来说并不明显,因此 - 令人困惑。

看起来您已经在数据库中创建了该表,但没有应用和注册它的迁移,因此当您尝试使用新列更新它时,它会生成“创建”迁移而不是“更新”迁移。

您需要先应用“创建”迁移,方法是使用 fh 选项 (fixHistory) 生成它,或者通过其他方式将其添加到 migration_history 表中。

【讨论】:

    【解决方案2】:

    要向表中添加列,您应该创建新的迁移。

    这是yii2中提供的例子guide

    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');
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 2013-04-09
      相关资源
      最近更新 更多