Когда я пытаюсь протестировать две свои модели на фабриках, появляется сообщение об ошибке, в котором говорится, что я не устанавливаю значение «тип».
SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: comment_votes.type (Connection: sqlite, SQL: insert into "comment_votes" ("comment_id", "user_id") values (1, 18), (2, 18), (3, 18), (4, 18), (5, 18))
В моей таблице comment_votes есть столбцы comment_id, user_id и type, причем все они не допускают значения NULL.Schema::create('comment_votes', function (Blueprint $table) {
$table->id();
$table->foreignId('comment_id')->constrained()->cascadeOnDelete();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->string('type');
$table->timestamps();
});
Я пытался проверить связь с Pest, используя приведенный ниже код
test('BelongsToMany relationship works', function () {
$user = User::factory()
->has(CommentVote::factory()->count(5), 'votedComments')
->create();
assertCount(5, $user->votedComments);
});
Моя модель Пользователь выглядит следующим образом:
class User extends Authenticatable
{
// Rest of the model...
/**
* Get the comments where the user has voted.
*/
public function votedComments(): BelongsToMany
{
return $this->belongsToMany(Comment::class, CommentVote::class);
}
}
Моя модель CommentVote выглядит следующим образом:
class CommentVote extends Model
{
use HasFactory;
protected $fillable = [
'comment_id',
'user_id',
'type', // Notice that is included
];
protected static function newFactory(): Factory
{
return CommentVoteFactory::new();
}
protected function casts(): array
{
return [
'type' => CommentVoteType::class,
];
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function comment(): BelongsTo
{
return $this->belongsTo(Comment::class);
}
}
Мой CommentVoteFactory выглядит так:
class CommentVoteFactory extends Factory
{
protected $model = CommentVote::class;
public function definition(): array
{
return [
'comment_id' => Comment::factory(),
'user_id' => User::factory(),
'type' => 'positive', // Notice that is included
];
}
}
Подробнее здесь: https://stackoverflow.com/questions/788 ... iled-in-la
Как я могу решить нарушение ограничения целостности: ограничение NOT NULL не удалось на фабриках Laravel ⇐ Php
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Ошибка целостности Django: ограничение NOT NULL не выполнено. Как исправить?
Anonymous » » в форуме Python - 0 Ответы
- 16 Просмотры
-
Последнее сообщение Anonymous
-
-
-
Ошибка целостности Django: ограничение NOT NULL не выполнено. Как исправить?
Anonymous » » в форуме Python - 0 Ответы
- 19 Просмотры
-
Последнее сообщение Anonymous
-