prg*_*grm 6 php mysql laravel eloquent
我试图使用Laravel Migration来创建SQL表,但它不会让我.
这是错误:
SQLSTATE [42S01]:基表或视图已存在:1050表'mytable'已存在
这是我的代码:
Schema::create('mytable', function (Blueprint $table) {
$table->increments('id');
$table->foreign('othertable_id')
->references('id')->on('othertable')
->onDelete('cascade');
$table->string('variable');
$table->timestamps();
});
}
{
Schema::drop('mytable');
}
Run Code Online (Sandbox Code Playgroud)
我还检查了其他迁移是否被称为"mytable",但它们不是,所以我不确定它来自何处.
我尝试了以下方法:
第一
php artisan migrate:refresh
Run Code Online (Sandbox Code Playgroud)
这给了我一个错误
然后我删除了整个数据库altogheter并使用:
php artisan migrate
Run Code Online (Sandbox Code Playgroud)
仍然有错误
Moh*_*sen 11
如果您在数据库中有该表并且不想迁移创建它,您可以通过在创建之前检查 Schema::hasTable 来忽略它
public function up()
{
if(Schema::hasTable('products')) return; //add this line to migration file
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
}
在laravel 5.5和>你可以使用:
$ php artisan migrate:fresh
// ( not migrate:refresh wich is a different command)
Run Code Online (Sandbox Code Playgroud)
此命令首先删除所有表,然后重新运行所有迁移