php - 为什么 laravel push 给我一个错误 1364:字段 'question_entity_id' 没有默认值?
问题描述
我对 Laravel 很陌生(这是我第一次使用它),我正在尝试将我在发布请求中创建的一些数据存储到我的 api 中。我不断收到一般错误:1364 字段“question_entity_id”没有默认值。
我正在尝试使用 Laravel 的push
方法来保存itemBank
模型以及我在下面定义的所有关系,但我得到了上面的错误。我已经尝试手动设置 foreign_key 关系,例如 `$itemBank->question_entity_id = $questionEntity->id' 但这给了我同样的错误。我特别想弄清楚为什么 question_entity_id 没有被填充(我知道可以通过使字段为空或给 question_entity_id 一个默认值来解决该错误)。
以下是相关型号:
class ItemBank extends Model
{
// table name
protected $table = "item_bank";
// do no use default timestamp fields
public $timestamps = false;
// item_bank relationships to other Models/tables
public function questionEntity() {
return $this->hasOne('App\QuestionEntity', 'id', 'question_entity_id');
}
public function optionEntity() {
return $this->hasMany('App\OptionEntity', 'item_id', 'id');
}
public function tagItemRel() {
return $this->hasOne('App\TagItemRel', 'item_id', 'id');
}
}
class QuestionEntity extends Model
{
// table name
protected $table = 'question_entity';
// disable default timestamps
public $timestamps = false;
public function itemBank() {
return $this->belongsTo('App\ItemBank', 'id', 'question_entity_id');
}
}
这是我尝试存储数据的代码:
public function store(Request $request)
{
$data = $request->all();
$itemBank = new ItemBank();
//save question body text
$questionEntity = new QuestionEntity();
$questionEntity->question = $data['questionBody'];
$itemBank->questionEntity()->save($questionEntity);
// save correct answer
$itemBank->correct_answers = $data['correctAnswer'];
//save question options
$choices = ['A', 'B', 'C', 'D'];
//$optionEntities = [];
foreach($choices as $choice) {
$optionEntity = new OptionEntity();
$optionEntity->choice = $data['choice' . $choice];
$optionEntity->choice_label = $choice;
$optionEntity->itemBank()->associate($itemBank);
}
//$itemBank->optionEntity()->saveMany($optionEntities);
//create new ItemTag Model
$itemTag = new ItemTag();
$itemTag->tag_name = $data['topic'];
//create new TagItemRel Model
$tagItemRel = new TagItemRel();
$tagItemRel->itemTag()->save($itemTag);
$tagItemRel->itemBank()->associate($itemBank);
$itemBank->push();
return $itemBank;
}
以下是相关的迁移文件:
QuestionEntity:
Schema::create('question_entity', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('question', 500);
});
ItemBank:
Schema::create('item_bank', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('question_entity_id');
$table->string('correct_answers', 1);
$table->foreign('question_entity_id')->references('id')->on('question_entity');
});
解决方案
添加question_entity_id
你的ItemBank
模型,像这样。
protected $fillable = [
'question_entity_id',
];
所以你的ItemBank
模型看起来像这样。
class ItemBank extends Model
{
// table name
protected $table = "item_bank";
protected $fillable = [
'question_entity_id','correct_answers'
];
推荐阅读
- angular - 版本 10 上的 Angular 材料日期选择器(日期范围功能)
- node.js - 关闭终端或控制台后永远无法正常工作
- javascript - 如何在 React 中为嵌套对象设置状态?
- android - 更新到 gradle 6.x variant.getVariantData().getVariantConfiguration() 后,Gradle 代码损坏
- reactjs - React + flask:如何使用命令行运行 API
- node.js - 节点身份验证,当我提交凭据时没有任何反应
- python - 拆分数据帧
- reactjs - React native:如何生成与稍后将呈现的组件大小相同的空白空格?
- sql-server - Invoke-SQLCMD 无法在 PowerShell 中运行多个 SQL 语句
- python - 如何在 python 中获取基于非源代码的模块的源代码?