【发布时间】: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