Laravel如何用外钥匙在桌子里播种?
我正在尝试使这两个最简单的桌子用涉及“父”表的外键使用,但是我似乎无法使其正常工作。 这是我的第一个表(用户):
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('toegewezen');
});
}
这是我的第二个表(Bestellingen):
public function up()
{
Schema::create('bestellingen', function (Blueprint $table) {
$table->increments('bestel_id');
$table->string("drank");
$table->string("categorie");
$table->integer('user_id')->unsigned(); // also tried nullable()
$table->foreign('user_id')->references('id')->on('users');
$table->integer("aantal");
$table->string("status");
$table->timestamp('created_at')->nullable();
});
}
当我迁移它时,一切看起来都不错,正是我尝试将数据实际播种到表中时,它会失败并给我以下错误代码:<代码>完整性约束违规:1452无法添加或更新子行:外键约束失败('ipmedth'。'Bestellingen',约束'BestelingEn_USER_ID_ID_FOREIGN_FOREING'forefer'forefer键('user_id'reformence'useres'cresences'users'('id')) (sql:插入“ bestellingen”(“ drank”,“ packorie”,“ user_id”,“ aantal”,“ status”)值(cola,frisdrank,1,1,1,besteld))。 我尝试查找错误,但我似乎找不到任何可以帮助我的东西。请告诉我我在这里做错了什么。这些是我的播种机:
用户餐桌播种机:
DB::table('users')->insert([
'name' => 'Pieter',
'toegewezen' => 'Nee'
]);
Bestellingen Table Seeder:
DB::table('bestellingen')->insert([
'drank' => 'Cola',
'categorie' => 'Frisdrank',
'user_id' => 1, // also tried with '1' and without entire line
'aantal' => 1,
'status' => "Besteld",
]);
I'm trying to make the 2 most simple tables work with a foreign key referring to the 'parent' table, but I just can't seem to get it to work.
This is my first table (users):
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('toegewezen');
});
}
This is my second table (bestellingen):
public function up()
{
Schema::create('bestellingen', function (Blueprint $table) {
$table->increments('bestel_id');
$table->string("drank");
$table->string("categorie");
$table->integer('user_id')->unsigned(); // also tried nullable()
$table->foreign('user_id')->references('id')->on('users');
$table->integer("aantal");
$table->string("status");
$table->timestamp('created_at')->nullable();
});
}
When I migrate this everything looks fine, it's just when I try to actually seed the data into the tables it fails and gives me the following error code: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ('ipmedth'.'bestellingen', CONSTRAINT 'bestellingen_user_id_foreign' FOREIGN KEY ('user_id') REFERENCES 'users' ('id')) (SQL: insert into 'bestellingen' ('drank', 'categorie', 'user_id', 'aantal', 'status') values (Cola, Frisdrank, 1, 1, Besteld))
.
I've tried looking up the error but I can't seem to find anything that can help me. Please tell me what I'm doing wrong here. These are my seeders:
Users table seeder:
DB::table('users')->insert([
'name' => 'Pieter',
'toegewezen' => 'Nee'
]);
Bestellingen table seeder:
DB::table('bestellingen')->insert([
'drank' => 'Cola',
'categorie' => 'Frisdrank',
'user_id' => 1, // also tried with '1' and without entire line
'aantal' => 1,
'status' => "Besteld",
]);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样编写外键“
$ table-&gt; foreferidfor(user :: class,'userId');
Write foreign key like this"
$table->foreignIdFor(User::class, 'userId');