Как я могу решить нарушение ограничения целостности: ограничение NOT NULL не удалось на фабриках LaravelPhp

Кемеровские программисты php общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Как я могу решить нарушение ограничения целостности: ограничение NOT NULL не удалось на фабриках Laravel

Сообщение Anonymous »

Когда я пытаюсь протестировать две свои модели на фабриках, появляется сообщение об ошибке, в котором говорится, что я не устанавливаю значение «тип».
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
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Php»