Laravel 5 如何在拉威尔放下桌子?
问题是我有以下错误: [例外情况] SQLSTATE[42S01]:基表或视图已存在:1050表“songs”已存在 这是我的迁移文件:Laravel 5 如何在拉威尔放下桌子?,laravel-5,Laravel 5,问题是我有以下错误: [例外情况] SQLSTATE[42S01]:基表或视图已存在:1050表“songs”已存在 这是我的迁移文件: <?php use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateSongsTable extends Migration { public function up() {
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSongsTable extends Migration
{
public function up()
{
Schema::create('songs', function (Blueprint $table)
{
$table->increments('id');
$table->integer('user_id');
$table->string('title');
$table->string('slug')->unique();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
}
在迁移过程中需要一个down方法,以便在运行php-artisan-migrate:rollback
时可以删除数据库
e、 g
我发现的一个简单的方法就是使用phpmyadmin并手动删除表。当然,如果再次运行迁移时迁移仍然存在,则会再次创建表。要删除表,可以使用Schema::drop方法:
Schema::drop('users');
//更好
Schema::dropIfExists('users');
要在laravel中删除表,请创建第一次迁移
删除表格的步骤
$ php artisan make:migration drop_user_table
将其添加到migrate file inside-up函数Schema::drop('tableName')代码>
然后跑
$ php artisan migrate
删除表并再次运行迁移的简单方法。只需运行artisan命令
php artisan migrate:fresh
注意:它将删除所有表并重新运行迁移
或者,如果您有表的种子,则运行此命令
php artisan migrate:fresh --seed
参考资料:您可以使用drop或dropIfExists方法:
Schema::drop('users');
Schema::dropIfExists('users');
如果要删除上次迁移的表,也可以回滚
php artisan migrate:rollback
migrate:reset命令将回滚应用程序的所有迁移:
php artisan migrate:reset
migrate:fresh命令将从数据库中删除所有表,然后执行migrate命令:
php artisan migrate:fresh
php artisan migrate:fresh --seed
如果您已经从迁移文件创建了表,那么只需回滚迁移,在cli php artisan migrate中运行以下命令:rollback我做到了,错误仍然存在显示您创建歌曲表的迁移文件在post中添加代码而不是在注释中,如果您像这样发布,没有人会阅读它,&您的文件中没有down函数,这就是回滚不起作用的原因编辑帖子并在那里添加代码,我将给出正确的代码这不是您的问题的解决方案,用“Laravel”来表达。我从未见过如此负面的分数!很好@瓦希德残忍!!谢谢你的正确和简洁的回答,但是,我更愿意看到的例子,删除一个比“用户”更不重要的表。可能是Schema::drop('widgets');和Schema::dropIfExists('widgets');会降低一些新手破坏安装的风险吗?我有一个包含数据的表,但我忘了将dropIfExists添加到我的透视表中,有没有办法在down方法中添加此dropIfExists字段而不丢失数据@alphaSchema::dropIfExists('users')
删除表
,因此,如果需要数据,在不备份表的情况下,无法删除它@faridvatanirollback命令应该是php-artisan-migrate:rollback
。
php artisan migrate:reset
php artisan migrate:fresh
php artisan migrate:fresh --seed