I have a Edtions and Issues tables, with a one to many relationship (issues N-1 editions).
When I try to call the create()
method on my Issue factory I get a NOT NULL violation:
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)).
However I am specifying an ID my 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,
];
}
}
My Seeder:
public function run()
{
Edition::each(function ($edition) {
Issue::factory()
->count(10)
->for($edition)
->create();
});
}
Issues table migration:
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');
});
relevant part of Issue model:
public $incrementing = false;
protected $table = 'issues';
protected $keyType = "string";
protected $primaryKey = "id";
protected $guarded = [];
protected $fillable = [
"id",
"edition_id",
"issuing_date"
];
What am I doing wrong here?