【发布时间】:2017-10-08 10:47:04
【问题描述】:
laravel 中的迁移文件是用来在数据库中创建表的,对吧?但是当我尝试迁移时,它会给我这个错误:
C:\xampp\htdocs\app>php artisan 迁移
[照亮\数据库\查询异常] SQLSTATE[42S01]:基表或视图已存在:1050 表“用户”已存在(SQL:创建表
users(idint unsigned not null auto_increment 主键,namevarchar(255) not null,@ 987654324@ varchar(255) 不为空,passwordvarchar(255) not null,remember_tokenvarchar(100) null,created_attimestamp null,updated_attim estamp null) 默认字符集 utf8mb4 collate utf8mb4_unicode_ci)[PDO异常] SQLSTATE[42S01]:基表或视图已存在:1050 表“用户”已存在
我创建了一个新的迁移文件,它被称为测试。我知道用户已经存在,但我想创建我创建的名为 test 的新表。
这是我将用于创建表但不会创建的迁移文件:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTestsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tests');
}
}
这是告诉我它存在的用户迁移文件:
<?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::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');
}
}
这里是密码迁移文件:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePasswordResetsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamp('created_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('password_resets');
}
}
这是我没有谈论的虚拟迁移文件,因为我认为这不是问题:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDummiesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('dummies', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('body');
$table->timestamp('date'); //if you dont put name for the timestamp it will create: create_at and update_at fields.
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('dummies');
}
}
很抱歉让你们久等了,我花了很长时间才找到编辑按钮并修复了我的代码间距。这是我第一次使用 stack over flow。
【问题讨论】: