Laravel 8 如何使用带有 UUID 主键的工厂播种?
Laravel 8 How to seed using Factories with UUID primary key?
我有版本和问题 tables,具有一对多关系(问题 N-1 版本)。
当我尝试在我的 Issue 工厂上调用 create()
方法时,我遇到了 NOT NULL 违规:
SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, 1, 12011981, null, 2021-09-01 23:09:17, 2021-09-01 23:09:17, null). (SQL: insert into "issues" ("id", "issuing_date", "edition_id", "updated_at", "created_at") values (?, 12011981, 1,
2021-09-01 23:09:17, 2021-09-01 23:09:17)).
但是我指定了一个 ID 我的 Issue Factory:
class IssueFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Issue::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$timestamp = mt_rand(1, time());
$randomDate = date("dmY", $timestamp);
return [
'id' => Str::uuid(),
'issuing_date' => $randomDate,
'edition_id' => 1,
];
}
}
我的播种机:
public function run()
{
Edition::each(function ($edition) {
Issue::factory()
->count(10)
->for($edition)
->create();
});
}
问题 table 迁移:
Schema::create('issues', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->integer('edition_id');
$table->string('issuing_date');
$table->timestamps();
$table->softDeletes();
$table->foreign('edition_id')->references('id')->on('editions');
});
问题模型的相关部分:
public $incrementing = false;
protected $table = 'issues';
protected $keyType = "string";
protected $primaryKey = "id";
protected $guarded = [];
protected $fillable = [
"id",
"edition_id",
"issuing_date"
];
我做错了什么?
您必须在 Schema
中使用如下所示
$table->uuid('id', 36)->primary();
在您的模型中使用
use Illuminate\Support\Str;
public $incrementing = false;
protected $keyType = 'string';
public static function boot(){
parent::boot();
static::creating(function ($issue) {
$issue->id = Str::uuid(36);
});
}
下面这行也添加到你的工厂
use Illuminate\Support\Str;
我有版本和问题 tables,具有一对多关系(问题 N-1 版本)。
当我尝试在我的 Issue 工厂上调用 create()
方法时,我遇到了 NOT NULL 违规:
SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, 1, 12011981, null, 2021-09-01 23:09:17, 2021-09-01 23:09:17, null). (SQL: insert into "issues" ("id", "issuing_date", "edition_id", "updated_at", "created_at") values (?, 12011981, 1,
2021-09-01 23:09:17, 2021-09-01 23:09:17)).
但是我指定了一个 ID 我的 Issue Factory:
class IssueFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Issue::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$timestamp = mt_rand(1, time());
$randomDate = date("dmY", $timestamp);
return [
'id' => Str::uuid(),
'issuing_date' => $randomDate,
'edition_id' => 1,
];
}
}
我的播种机:
public function run()
{
Edition::each(function ($edition) {
Issue::factory()
->count(10)
->for($edition)
->create();
});
}
问题 table 迁移:
Schema::create('issues', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->integer('edition_id');
$table->string('issuing_date');
$table->timestamps();
$table->softDeletes();
$table->foreign('edition_id')->references('id')->on('editions');
});
问题模型的相关部分:
public $incrementing = false;
protected $table = 'issues';
protected $keyType = "string";
protected $primaryKey = "id";
protected $guarded = [];
protected $fillable = [
"id",
"edition_id",
"issuing_date"
];
我做错了什么?
您必须在 Schema
$table->uuid('id', 36)->primary();
在您的模型中使用
use Illuminate\Support\Str;
public $incrementing = false;
protected $keyType = 'string';
public static function boot(){
parent::boot();
static::creating(function ($issue) {
$issue->id = Str::uuid(36);
});
}
下面这行也添加到你的工厂
use Illuminate\Support\Str;