gpt4 book ai didi

php - 拉拉维尔 : seeder with foriegn keys

转载 作者:搜寻专家 更新时间:2023-10-31 21:23:21 25 4
gpt4 key购买 nike

在我的数据库中,我已经有了表:通知表、状态表,它们有多对多的关系,这就是为什么我有一个名为 notification_status 的数据透视表。我用迁移创建它们并用播种机播种它们,一切正常。现在我意识到我需要一个额外的表,它与通知表有多对一的关系(natification->hasMany->alertfrequency)。当我尝试迁移它时,它确实允许我这样做。这是我的通知表

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateNotificationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('notifications', function (Blueprint $table) {
$table->increments('id');
$table->string('website_url');
$table->string('email');
$table->string('slack_channel');
$table->string('check_frequency');
$table->string('alert_frequency');
$table->string('speed_frequency');
$table->boolean('active');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notifications');
}
}

还有警报频率表,我要添加的新表,

 <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAlertFrequenciesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('alertFrequencies', function (Blueprint $table) {
$table->increments('id');
$table->integer('notification_id');
$table->foreign('notification_id')
->references('id')->on('notifications')
->onDelete('cascade');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('alertFrequencies');
}
}

当我尝试添加时,我得到以下约束

  [Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `alertFrequencies` add constraint `alertfrequencies_notification_id_foreign` foreign key (`notification_
id`) references `notifications` (`id`) on delete cascade)

任何有想法或建议的人。我感谢所有想法和建议。

最佳答案

来自@AlexeyMezenin 的回答,更新基于Documentation :替换

$table->integer('notification_id')->unsigned();

$table->unsignedInteger('notification_id');

关于php - 拉拉维尔 : seeder with foriegn keys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41632979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com