0%

Laravel数据库迁移

记一次心力交瘁的laravel数据库迁移。

一、复现过程

  1. 安装composer
  2. 创建laravel项目 composer create-project laravel/laravel laravel_migrate
  3. cd laravel_migrate
  4. php artisan make:migration create_shop_order
    • 2021_03_25_152312_create_shop_order.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateShopOrder extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('shop_order', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('shop_order');
}
};
  1. 修改.env文件,主要是MySQL
  2. php artisan migrate
  1. 突然发现发现命名不符合规范

    • 手动修改2021_03_25_152312_create_shop_order.php,改为2021_03_25_152312_create_shop_order_table.php
  2. 去MySQL删除已经生成的表

  3. 再次执行php artisan migrate,报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
INFO  Running migrations.

2014_10_12_000000_create_users_table .................................................. 99ms DONE
2014_10_12_100000_create_password_resets_table ........................................ 97ms DONE
2019_08_19_000000_create_failed_jobs_table ............................................ 92ms DONE
2019_12_14_000001_create_personal_access_tokens_table ................................ 139ms DONE
PHP Fatal error: Cannot declare class CreateShopOrder, because the name is already in use in /Users/liuyulong/laravel_app/database/migrations/2021_03_25_152312_create_shop_order_table.php on line 7

Symfony\Component\ErrorHandler\Error\FatalError

Cannot declare class CreateShopOrder, because the name is already in use

at database/migrations/2021_03_25_152312_create_shop_order_table.php:7
3▕ use Illuminate\Database\Migrations\Migration;
4▕ use Illuminate\Database\Schema\Blueprint;
5▕ use Illuminate\Support\Facades\Schema;
6▕
➜ 7▕ class CreateShopOrder extends Migration
8▕ {
9▕ /**
10▕ * Run the migrations.
11▕ *


Whoops\Exception\ErrorException

Cannot declare class CreateShopOrder, because the name is already in use

at database/migrations/2021_03_25_152312_create_shop_order_table.php:7
3▕ use Illuminate\Database\Migrations\Migration;
4▕ use Illuminate\Database\Schema\Blueprint;
5▕ use Illuminate\Support\Facades\Schema;
6▕
➜ 7▕ class CreateShopOrder extends Migration
8▕ {
9▕ /**
10▕ * Run the migrations.
11▕ *

+1 vendor frames
2 [internal]:0
Whoops\Run::handleShutdown()
  1. 于是乎你开始百度,GG,stackoverflow…找了一圈就这几个方案

    • php artisan optimize:clear
    • composer dump-autoload
    • composer clear-cache
    • Don’t rename the original migration files,If you must rename the migration, also make sure to rename the class too!
  2. 一圈试下来发现并不能解决任何问题,于是乎你就开始怀疑起了人生,人为什么要活着,我什么要干程序员,地球怎么还不毁灭…

二、解决

  1. 使用anonymous migration support,8.37+支持
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateShopOrders extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('shop_order', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('shop_order');
}
};

三、参考

  1. 参考一
  2. 参考二
  3. 参考三
  4. 参考四