Когда я пытаюсь протестировать две свои модели на фабриках, появляется сообщение об ошибке, в котором говорится, что я не устанавливаю значение «тип».
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
Кемеровские программисты php общаются здесь
1722910830
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);
});
Моя модель [b]Пользователь[/b] выглядит следующим образом:
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);
}
}
Моя модель [b]CommentVote[/b] выглядит следующим образом:
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);
}
}
Мой [b]CommentVoteFactory[/b] выглядит так:
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
];
}
}
Подробнее здесь: [url]https://stackoverflow.com/questions/78836992/how-can-i-solve-integrity-constraint-violation-not-null-constraint-failed-in-la[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия