[英]I can't migrate for some reason in Laravel
laravel中的迁移文件用于在数据库中创建表,对吗? 但是,每当我尝试迁移时,都会出现此错误:
C:\\ xampp \\ htdocs \\ app> php artisan迁移
[Illuminate \\ Database \\ QueryException] SQLSTATE [42S01]:基本表或视图已存在:1050表'users'已存在(SQL:创建表
users
(id
int unsigned不为null auto_increment主键,name
varchar(255)不为null,password
为varchar(255)NOT NULL,remember_token
为varchar(100)空,created_at
时间戳空,updated_at
添estamp空)默认字符集utf8mb4整理utf8mb4_unicode_ci)[PDOException] SQLSTATE [42S01]:基表或视图已存在:1050表“用户”已存在
我创建了一个新的迁移文件,称为测试。 我知道用户已经存在,但是我想创建自己创建的新表,称为test。 我删除了所有表并重新迁移,但它仅再次创建了我的用户和迁移表。 不是我想要创建的新表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');
}
}
您正在使用此命令吗? php artisan migrate:refresh
如果您这样做,请尝试以下操作:
<?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');
}
}
并在您的其他迁移文件中执行相同的操作,这样,该表将在每次迁移之前删除,并且您不再遇到问题了:)(与php artisan migrate:refresh
migration完全相同php artisan migrate:refresh
我认为您应该尝试这样做:
首先从数据库中的迁移表中删除测试,用户,password_resets,虚拟变量,然后运行php artisan migrate
命令
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.