【问题标题】:php artisan db:seed using laravel error when creating Faker for table that have foreign keysphp artisan db:seed 在为具有外键的表创建 Faker 时使用 laravel 错误
【发布时间】:2019-05-28 19:12:02
【问题描述】:

database/migrations/2018_12_20_022430_create_products_table.php

> class CreateProductsTable extends Migration {
>     /**
>      * Run the migrations.
>      *
>      * @return void
>      */
>     public function up()
>     {
>         Schema::create('products', function (Blueprint $table) {
>             $table->increments('id');
>             $table->string ('name');
>             $table->text('description');
>             $table->decimal('price');
>             $table->string('file');
>             $table->timestamps();
>         });
>     }

数据库/迁移/2018_12_20_042857_create_cards_table.php

class CreateCardsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('cards', function (Blueprint $table) {
            $table->increments('id');
            $table->string ('quantity');
            $table->string('status');
            $table->integer('pid')->unsigned();
            $table->foreign('pid')->references('id')->on('products');
            $table->integer('cid')->unsigned();
            $table->foreign('cid')->references('id')->on('customers');

            $table->timestamps();
        });
    }

数据库/工厂/UserFactory.php

$factory->define(App\Product::class, function (Faker $faker) { 
    return [
              //'id'=>$faker->numberBetween($min = 1, $max = 20),
              'name'=> $faker->word,
              'description'=> $faker->sentence($nbWords = 6, $variableNbWords = true),
              'price' => $faker->randomFloat($nbMaxDecimals = 100, $min = 1, $max = 10),
              'file' => $faker->imageUrl($width = 640, $height = 480),
    ];
});


$factory->define(App\Card::class, function (Faker $faker) {
    return [
               //'id'=>$faker->numberBetween($min = 1, $max = 20),
               'quantity'=>$faker->sentence($nbWords = 6, $variableNbWords = true),
               'status'=>$faker->boolean($chanceOfGettingTrue = 10),
               'cid'=>$faker->numberBetween($min = 1, $max = 20),
              'pid'=>$faker->numberBetween($min = 1, $max = 20),
    ];
});

routes/web.php

factory(App\Product::class,5)->create();
factory(App\Card::class,5)->create();

终端:

$ php artisan db:seed

错误:

在 Connection.php 第 664 行: SQLSTATE [23000]:完整性约束违规:1452 无法添加或 更新子行:外键约束失败 (food.cards, 约束ca rds_pid_foreign 外键 (pid) 参考products (id)) (SQL: 插入cards (quantity, status, cid, pid, updated_at, creat ed_at) 价值观 (Vitae asperiores eligendi ipsam exercitationem quidem., 1,
18, 8, 2019-01-02 04:22:10, 2019-01-02 04:22:10)))

在 Connection.php 第 458 行: SQLSTATE [23000]:完整性约束违规:1452 无法添加或 更新子行:外键约束失败 (food.cards,约束ca rds_pid_foreign外键 (pid) 参考资料products (id))

【问题讨论】:

  • 您不能将随机数用作pid,因为它期望在产品表的 id 列中存在一个外键。
  • 那么我如何将 pid 与它的源表关联起来?

标签: php mysql laravel


【解决方案1】:

您必须使用产品和客户的 ID 而不是随机数:

$factory->define(App\Card::class, function (Faker $faker) {
    $p_ids = App\Product::pluck('id')->toArray();
    $c_ids = App\Customer::pluck('id')->toArray();
    return [
               //'id'=>$faker->numberBetween($min = 1, $max = 20),
               'quantity'=>$faker->sentence($nbWords = 6, $variableNbWords = true),
               'status'=>$faker->boolean($chanceOfGettingTrue = 10),
               'cid'=>$faker->randomElement($c_ids),
               'pid'=> $faker->randomElement($p_ids),
    ];
});

简而言之,从products 和customers 表中选择所有id,并从中取一个随机值作为外键。

但请确保您的工厂按特定顺序排列,因此必须在 ProductFactory 和 CustomerFactory 之后解雇 CardFactory。

【讨论】:

  • $ php artisan db:seed 数据库播种成功完成。
  • shokran ya ahmed
【解决方案2】:

如果pid 具有随机值,但数据库不会在cards table 中向pid 输入一个在products table 中不存在的值。 pid 中插入的值必须存在于product id 中。

【讨论】:

  • 是的,我明白这一点,答案是声明 pid 和 cid,然后在播种之前将它们连接起来,如上面的答案中所述,谢谢您的帮助
猜你喜欢
  • 1970-01-01
  • 2015-09-14
  • 2015-06-25
  • 2015-11-11
  • 2016-07-12
  • 2014-05-10
  • 2021-08-21
  • 2020-08-07
  • 2016-11-25
相关资源
最近更新 更多