php - 在 Laravel 8 中获取“错误地通知外键约束”
问题描述
我正在尝试使用 foreignId 从另一个表中获取 ID,但它不会让我这样做。
我的用户表:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id('user_id');
$table->string('first_name', 40);
$table->string('last_name', 40);
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->foreignId('tier_id')->references('tier_id')->on('tiers');
$table->rememberToken();
$table->timestamps();
});
}
我的等级表:
public function up()
{
Schema::create('tiers', function (Blueprint $table) {
$table->id('tier_id');
$table->string('name');
$table->float('price');
$table->string('max_resolution');
$table->integer('max_users');
$table->timestamps();
});
}
我做错了什么,请帮忙!
解决了,方法如下:Espresso 的评论似乎有帮助,迁移完美完成,我需要做的就是更改文件名。谢谢!
解决方案
你能像那个父表一样尝试你的迁移吗?
public function up()
{
Schema::create('tiers', function (Blueprint $table) {
$table->increments('tier_id');
$table->string('name');
$table->float('price');
$table->string('max_resolution');
$table->integer('max_users');
$table->timestamps();
});
}
子表
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('users_id');
$table->string('first_name', 40);
$table->string('last_name', 40);
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->integer('tier_id')->unsigned()->nullable();
$table->rememberToken();
$table->timestamps();
$table->foreign('tier_id')->references('tier_id')->on('tiers');
});
}
推荐阅读
- c - 如何在C目录中列出目录中的文件?
- python - Transformer 的输入和输出分别是什么?
- vb.net - 当公差以 ±2 之类的格式提供时,如何评估整数或小数是否在公差范围内?
- c# - 附加时间戳并移动文件 C#
- operating-system - 捕捉程序的信号
- django - 当对 localhost:121/logout 进行 GET 请求时,我得到“accounts.views.logout 没有返回 HttpResponse 对象”。这是一个问题吗?
- javascript - 如何在三元 + 箭头符号中插入“for”
- python - 使用变量访问 Python 字典的键
- c - 为什么在下面的程序中添加了“\0”?
- c# - dotnet\sdk\3.0.100\NuGet.targets(123,5):错误:项目名称不明确