我是Laravel初学者,我试图建立一个简单的数据库。这个问题是关于外键的一个问题。我看了文档,但我没有找到任何解决方案。
这是错误: SQLSTATE23000:完整性约束冲突: 1452无法添加或更新子行:外键约束失败(vlogit。posts,constraint posts_user_id_foreign外键(user_id)引用users (id) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert insert posts (user_id,category_id,name,slug,excerpt,body,file,status,updated_at,created_at)值(28,18,Voluptas qui - ut.,voluptas-qui-ut,voluptas-qui-ut,我买了一辆大车。最低限度的指控是偶然发生的。最好的解决办法是。他说:“我不知道你的工作是什么,我的工作是什么?”劳力等人指控他的身份。结论:我的意思是我的收入是最好的。这是一种新的方法。最大的问题就是。莫列蒂亚·伊莱姆指责我坐在非易懂的地方。你逃亡的后果是什么。天鹅绒和羊毛绒。没有人知道我的工作是什么。这是一种伪装的说法。Ea sit ad officiis quam,https://via.placeholder.com/1200x400.png/0066cc?text=voluptas,发布,2020-11-21 23:46:59,2020-11-21 23:46:59)
数据库是关于一个简单的博客,与用户,类别,帖子和标签列。数据库可以正确地处理帖子、类别和用户,但不能处理post_tags。我认为这个错误是这样产生的:
The migration:post_tag_table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostTagTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('post_tag', function (Blueprint $table) {
$table->increments('id');
$table->integer('post_id')->unsigned();
$table->integer('tag_id')->unsigned();
$table->timestamps();
//Relaciones
$table->foreign('post_id')->references('id')->on('posts')
->onDelete('cascade')
->onUpdate('cascade');
$table->foreign('tag_id')->references('id')->on('tags')
->onDelete('cascade')
->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('post_tag');
}
}PostFactory.php
<?php
namespace Database\Factories;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class PostFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Post::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$title = $this->faker->sentence(4);
return [
'user_id' => rand(1,30),
'category_id' => rand(1,20),
'name' => $title,
'slug' => Str::slug($title),
'excerpt' => $this->faker->text(200),
'body' => $this->faker->text(500),
'file' => $this->faker->imageUrl($width = 1200, $height = 400),
'status' =>$this->faker->randomElement(['DRAFT', 'PUBLISHED']),
];
}
}TagFactory.php
<?php
namespace Database\Factories;
use App\Models\Tag;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class TagFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Tag::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$title = $this->faker->sentence(4);
return [
'name' => $title,
'slug' => Str::slug($title),
];
}
}PostTableSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class PostsTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//
\App\Models\Post::factory(300)->create()->each(function(\App\Models\Post $post) {
$post->tags()->attach([
rand(1,5),
rand(6,14),
rand(15,20)
]);
});
}
}发布于 2020-11-22 18:12:57
正如@lagbox所指出的,users表中应该有30条记录,ids在1-30之间。
然而,更合适的方式来定义后工厂-拉威尔方式是通过工厂'user_id' => \App\Models\User::factory()利用关系
<?php
namespace Database\Factories;
use App\Models\Post;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class PostFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Post::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$title = $this->faker->sentence(4);
return [
'user_id' => \App\Models\User::factory(),
'category_id' => rand(1,20),
'name' => $title,
'slug' => Str::slug($title),
'excerpt' => $this->faker->text(200),
'body' => $this->faker->text(500),
'file' => $this->faker->imageUrl($width = 1200, $height = 400),
'status' =>$this->faker->randomElement(['DRAFT', 'PUBLISHED']),
];
}
}另一件需要记住的事情是迁移的顺序。
发布于 2020-11-22 19:07:19
解决了。这是通用种子文件中的一个问题。
DatabaseSeeder.php
$this->call(UsersTableSeeder::class);
$this->call(CategoriesTableSeeder::class);
$this->call(TagsTableSeeder::class);
$this->call(PostsTableSeeder::class);现在可以正常工作了。
CategoriesTableSeeder.php
public function definition()
{
$title = $this->faker->sentence(4);
return [
'name' => $title,
'slug' => Str::slug($title),
'body' => $this->faker->text(500),
];
}PostTableSeeder.php
public function definition()
{
$title = $this->faker->sentence(4);
return [
'user_id' => rand(1,30),
'category_id' => rand(1,30),
'name' => $title,
'slug' => Str::slug($title),
'excerpt' => $this->faker->text(200),
'body' => $this->faker->text(500),
'file' => $this->faker->imageUrl($width = 1200, $height = 400),
'status' =>$this->faker->randomElement(['DRAFT', 'PUBLISHED']),
];
}TagTableSeeder.php
public function definition()
{
$title = $this->faker->unique()->word(4);
return [
'name' => $title,
'slug' => Str::slug($title),
];
}我分享这些代码是为了帮助其他初学者。
https://stackoverflow.com/questions/64952741
复制相似问题