Da'sBlog

laravel-migration

php artisan make:migration wechat_admin –create=admin

php artisan make:migration create_userNAME 后面跟行为 和表名

错误信息

1
2
3
4
5
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes (SQL: alter table users add unique users_email_unique(email))
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

原因
laravel匹配的是新版本的mysql.而旧版本的mysql长度不够。

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
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}

Error Base table or view already exists: 1050 Table ‘users’ already exists

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
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::dropIfExists('users');
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}

第一种,如果刚刚建立起table,并没有数据,可以选择rollback或者reset,即使用

$ php artisan migrate:rollback
1
或者

$ php artisan migrate:refresh
1
值得注意的是,rollback这个命令默认选项只会向之前恢复一次migration,而refresh将会把所有migration全部重置,相当于

$ php artisan migrate:reset
$ php artisan migrate

如果迁移已经执行,先回滚
php artisan migrate:rollback

然后删除迁移文件,运行
composer dump-autoload

php artisan migrate:reset

坚持原创技术分享,您的支持将鼓励我继续创作!