Php 如何通过外键laravel
我试图传递外键,但遇到此错误Php 如何通过外键laravel,php,laravel,laravel-5,Php,Laravel,Laravel 5,我试图传递外键,但遇到此错误基本表或视图已存在:1050表“收藏夹”已存在“ 如何传递所有外键和普通id,如$table->biginger('user_id')->unsigned(); 桌子 public function up() { Schema::create('favorites', function (Blueprint $table) { $table->Increments('id'); $table->bigIntege
基本表或视图已存在:1050表“收藏夹”已存在“
如何传递所有外键和普通id,如$table->biginger('user_id')->unsigned();
桌子
public function up()
{
Schema::create('favorites', function (Blueprint $table) {
$table->Increments('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('product_id')->unsigned();
$table->timestamps();
});
Schema::create('favorites', function (Blueprint $table){
$table->bigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users');
$table->bigInteger('product_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('products');
});
}
public function down()
{
Schema::dropIfExists('favorites');
}
如果要迁移表两次,请在同一次迁移中传递外键
public function up()
{
Schema::create('favorites',函数(Blueprint$表){
$table->bigIncrements('id');
$table->biginger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->biginger('product_id')->unsigned();
$table->foreign('product_id')->引用('id')->关于('products');
$table->timestamps();
});
}
公共职能部门
{
Schema::dropIfExists('favorites');
}
希望这有帮助其他答案都是正确的。如果你在做之前错过了所需的任务,请尝试一下这个 1) 删除或删除已迁移的表 2) 在迁移表中,删除包含名称“收藏夹”的记录 3) 将该代码复制并粘贴到“up”函数中
Schema::create('favorites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('product_id')->unsigned();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('product_id')->references('id')->on('products');
});
4) 再次运行此命令
php artisan migrate
将第二个块移到第一个块的内部。同一个表不能有两个
create
块。请将第二个Schema::create
更改为Schema::table
,因为此时您不是在创建新表,而是在修改表