运行php artisan迁移命令时出错
我试图在我的博客应用程序中添加评论系统,但在尝试运行评论迁移时出现此错误,该迁移似乎与我当前的运行php artisan迁移命令时出错,php,laravel,mariadb,laravel-5.3,database-migration,Php,Laravel,Mariadb,Laravel 5.3,Database Migration,我试图在我的博客应用程序中添加评论系统,但在尝试运行评论迁移时出现此错误,该迁移似乎与我当前的评论迁移文件无关,而与以前的post_标记迁移文件无关 [Illuminate\Database\QueryException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'post_tag' already exists (SQL: create table `post_tag` ( `id` in
评论
迁移文件无关,而与以前的post_标记
迁移文件无关
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'post_tag' already exists (SQL:
create table `post_tag` (
`id` int unsigned not null auto_increment primary key,
`post_id` int unsigned not null,
`tag_id` int unsigned not null
) default character set utf8 collate utf8_unicode_ci)
<?php
//2017_01_16_101128_create_comments_table.php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->text('comment');
$table->boolean('approved');
$table->integer('post_id')->unsigned();
$table->timestamps();
});
Schema::table('comments', function($table){
$table->foreign('post_id')->references('id')->on('posts')->
onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropForeign(['post_id']);
Schema::drop('comments');
}
}
<?php
2016_12_18_230831_create_post_tag_table.php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostTagTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('post_tag', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts');
$table->integer('tag_id')->unsigned();
$table->foreign('tag_id')->unsigned();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('post_tag');
}
}
这是我的注释
迁移文件
[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'post_tag' already exists (SQL:
create table `post_tag` (
`id` int unsigned not null auto_increment primary key,
`post_id` int unsigned not null,
`tag_id` int unsigned not null
) default character set utf8 collate utf8_unicode_ci)
<?php
//2017_01_16_101128_create_comments_table.php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCommentsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->text('comment');
$table->boolean('approved');
$table->integer('post_id')->unsigned();
$table->timestamps();
});
Schema::table('comments', function($table){
$table->foreign('post_id')->references('id')->on('posts')->
onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropForeign(['post_id']);
Schema::drop('comments');
}
}
<?php
2016_12_18_230831_create_post_tag_table.php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostTagTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('post_tag', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts');
$table->integer('tag_id')->unsigned();
$table->foreign('tag_id')->unsigned();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('post_tag');
}
}
如何消除此错误或我在这里遗漏了什么?您必须在mysql中手动删除该表:
mysql> drop table post_tag;
然后再次运行迁移
php artisan migrate
您必须在mysql中手动删除该表:
mysql> drop table post_tag;
然后再次运行迁移
php artisan migrate
在
CreatePostTagTable
迁移中,您可能不会更改
$table->integer('tag_id')->unsigned();
$table->foreign('tag_id')->unsigned();
到
有一个重复
然后手动移除post_标签
表格。它本可以再次创建。您可能还需要检查数据库中的迁移
表,因为可能存在迁移的记录<代码>后标签表格。如果是,请将其移除。这样您就可以安全地运行迁移
此外,由于您正在创建一个
透视表
,因此可能不需要$table->increments('id')代码>。这可能取决于你的情况。在大多数情况下,您不需要它。在您的CreatePostTagTable
迁移中,您可能不会更改它
$table->integer('tag_id')->unsigned();
$table->foreign('tag_id')->unsigned();
到
有一个重复
然后手动移除post_标签
表格。它本可以再次创建。您可能还需要检查数据库中的迁移
表,因为可能存在迁移的记录<代码>后标签
表格。如果是,请将其移除。这样您就可以安全地运行迁移
此外,由于您正在创建一个
透视表
,因此可能不需要$table->increments('id')代码>。这可能取决于你的情况。在大多数情况下,你不需要它。我认为你应该试试这个:
Drop table post_tag;
和
从迁移表中删除后标记迁移
运行迁移之后
php artisan migrate
希望这项工作为你 我想你应该试试这个:
Drop table post_tag;
和
从迁移表中删除后标记迁移
运行迁移之后
php artisan migrate
希望这项工作为你 我试着按照你的建议去做,现在我得到了错误“[illumb\Database\QueryException]SQLSTATE[42000]:语法错误或访问冲突:1064你的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以了解在第1行(SQL:alter tablepost\uTag
add constraintpost\uTag\uID\uForeign
foreign key(tag\uID
)引用“`()”)附近使用的正确语法,如果数据库没有生产数据且项目正在开发中,您最好删除整个数据库,创建它,进行Gayan在下面建议的更改,然后运行php artisan Migrate。我尝试按照您的建议执行。现在我收到错误“[Illumb\database\QueryException]SQLSTATE[42000]:语法错误或访问冲突:1064您的SQL语法有错误;检查与您的MariaDB服务器版本相对应的手册,以了解在第1行(SQL:alter tablepost\uTag
add constraintpost\uTag\uID\uForeign
foreign key(tag\uID
)引用“`()”)附近使用的正确语法,如果数据库没有生产数据且项目正在开发中,最好删除整个数据库,创建它,进行Gayan在下面建议的更改,然后运行php artisan migrate