首页 > 解决方案 > 为什么 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');
        });

标签: phplaravel-5posteloquent

解决方案


添加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'
];

推荐阅读