Not sure what is happening here. I have a table called news
as follows:
Schema::create('news', function(Blueprint $table) {
$table->increments('id');
$table->unsignedBigInteger('customer_id');
$table->string('title');
$table->string('featured_img');
$table->text('body');
$table->enum('audience', ['all', 'users', 'customers', 'residents', 'plumbers'])->default('users');
$table->timestamp('published_at')->nullable();
$table->timestamp('featured_from')->nullable();
$table->timestamp('featured_to')->nullable();
$table->timestamps();
$table->foreign('customer_id')
->references('id')
->on('customers');
});
I have a factory for news
:
class NewsFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = News::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$audience = [
'all', 'users', 'customers', 'residents', 'plumbers',
];
$img = [
'https://images.unsplash.com/photo-1492724441997-5dc865305da7?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1679&q=80',
'https://images.unsplash.com/photo-1547586696-ea22b4d4235d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1679&q=80',
'https://images.unsplash.com/photo-1496128858413-b36217c2ce36?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1679&q=80',
];
$date = now()->subMonths(rand(1, 20));
$randAudience = $audience[array_rand($audience)];
$randImg = $img[array_rand($img)];
$customer = Customer::where('parent_id', 1)->orWhereNull('parent_id')->inRandomOrder()->first();
echo "c => " . $randAudience . "
";
return [
'customer_id' => $customer->id,
'audience' => $randAudience,
'private_customer_id' => null,
'title' => $this->faker->title,
'body' => $this->faker->realText,
'featured_img' => $randImg,
'published_at' => $date,
'featured_from' => $date,
'featured_to' => $date->addDays(14),
];
}
}
I then call it in a seeder like this:
public function run()
{
News::factory()
->count(3)
->create();
}
It shows me it runs the 3 times, then seems to fail on the fourth iteration (?) with the following error:
c => residents
c => all
c => users
IlluminateDatabaseQueryException with message 'SQLSTATE[23514]: Check violation: 7 ERROR: new row for relation "news" violates check constraint "news_audience_check"
Any help is appreciated
UPDATE: From the Postgresql console, the d news
output per @IGP suggestion. The check is the ENUM
validation.
Check constraints:
"news_audience_check" CHECK (audience::text = ANY (ARRAY['all'::character varying, 'users'::character varying, 'customers'::character varying, 'residents'::character varying, 'plumbers'::character varying]::text[]))
Additionally, if I comment out the random assignment of an audience value (and allow the default to be set) then it works.
// 'audience' => $randAudience,
Using a random value it triggers the check on n+1 iteration (why a 4th iteration when I asked for 3 instances?). Also, the error dump (below) shows the audience
column of n+1 record to contain residents
which is a valid entry.
SQLSTATE[23514]: Check violation: 7 ERROR: new row for relation "news" violates check constraint "news_audience_check"
DETAIL: Failing row contains (1, b9f00221-49f8-4d92-9813-026206e6aa76, 2, Mr., https://images.unsplash.com/photo-1547586696-ea22b4d4235d?ixlib=..., Mouse. '--I proceed. "Edwin and Morcar, the earls of Mercia and ..., "residents", null, 2020-10-23 23:35:58, 2020-10-23 23:35:58, 2020-10-23 23:35:58, 1, 1, null, 2021-01-09 23:35:59, 2021-01-09 23:35:59). (SQL: insert into "news" ("customer_id", "audience", "private_customer_id", "title", "body", "featured_img", "published_at", "featured_from", "featured_to", "uuid", "creator_id", "updater_id", "updated_at", "created_at") values (2, "residents", ?, Mr., Mouse. '--I proceed. "Edwin and Morcar, the earls of Mercia and Northumbria, declared for him: and even Stigand, the patriotic archbishop of Canterbury, found it advisable--"' 'Found WHAT?' said the., https://images.unsplash.com/photo-1547586696-ea22b4d4235d?ixlib=rb-.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1679&q=80, 2020-10-23 23:35:58, 2020-10-23 23:35:58, 2020-10-23 23:35:58, b9f00221-49f8-4d92-9813-026206e6aa76, 1, 1, 2021-01-09 23:35:59, 2021-01-09 23:35:59) returning "id")
question from:
https://stackoverflow.com/questions/65643017/laravel-8-factory-migration-fails-on-n-1-iteration