违反完整性约束:1048 列“school_id”不能为空

Posted

技术标签:

【中文标题】违反完整性约束:1048 列“school_id”不能为空【英文标题】:Integrity constraint violation: 1048 Column 'school_id' cannot be null 【发布时间】:2020-03-29 19:44:40 【问题描述】:

当我尝试以 Json 格式将数据发布到 api 时,我收到错误:

“违反完整性约束:1048 列 'school_id' 不能为空”

这是我的 js (vuejs)

if (this.editD === false) 
    fetch('/api/school/'+this.location.school_id+'/location/store', 
        method: 'post',
        body: JSON.stringify(this.location),
        header: 
            'content-type': 'application/json'
        
    )
    .then(res => res.json())
    .then(data => 
        this.emptyLocation();
        this.editD = false;
        this.fetchSchools();
    )
    .catch(err => console.log(err));

我尝试返回正在发送的正文值,以验证它是否是有效的 Json 格式并且是:


    "id": "",
    "school_id": 1,
    "department_name": "test",
    "department_address": "etst",
    "department_zip": "ttest",
    "department_city": "set",
    "department_phone": "32324"

我的 Api 路由如下所示:

Route::post('/school/id/location/store', 'Owner\ApiController@storeLocation');

控制器方法是这样的:

public function storeLocation(request $request) 

    $department = new Department;
    $department->school_id = $request->input('school_id');
    $department->department_name = $request->input('department_name');
    $department->department_address = $request->input('department_address');
    $department->department_zip = $request->input('department_zip');
    $department->department_city = $request->input('department_city');
    $department->department_phone = $request->input('department_phone');

    if($department->save()) 
        return new Department($department);
    


在 Postmen 中使用相同的参数(但不在 json 中)执行此帖子时,它确实将其插入到数据库中,但我收到以下错误:

“传递给 Illuminate\Database\Eloquent\Model::__construct() 的参数 1 必须是数组类型,给定对象”

【问题讨论】:

【参考方案1】:

我相信这是因为你在给它一个 school_id 之前更新了一个Department

public function storeLocation(request $request) 

    $department = Department::create($request->all());

    return $department;


还要确保您的部门模型具有以下内容

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'school_id',
        'department_name',
        'department_address',
        'department_zip',
        'department_city',
        'department_phone',
    ];

您提供的代码即使在创建一个新部门后也会返回一个新部门。

编辑:

尝试将您的帖子修改为以下内容

    const location = this.location;
    fetch('/api/school/'+this.location.school_id+'/location/store', 
        method: 'post',
        body: 
           ...location,
        ,
        header: 
            'content-type': 'application/json'
        
    )

【讨论】:

将方法更改为您的代码并添加大量可分配属性。现在我收到此错误:一般错误:1364 Field 'school_id' doesn't have a default value (SQL: insert into department (updated_at, created_at) 值 (2019-12-04 22:57:29 , 2019-12-04 22:57:29) 当你dd($request->all());时会发生什么 它是空的。它返回:[] @rubex01 我已经更新了我的答案,我认为是因为您正在对帖子进行字符串化,它应该仍然是一个对象。 如果标头内容类型仍然是 application/json,我如何将对象作为正文发送。当将对象作为主体时,它表示有效负载是 [object Object]

以上是关于违反完整性约束:1048 列“school_id”不能为空的主要内容,如果未能解决你的问题,请参考以下文章

SQLSTATE [23000]:完整性约束违规:1048 laravel 5.7

Laravel Eloquent:违反完整性约束:1052 列“id”在 where 子句不明确

Laravel JOIN SQLSTATE [23000] 问题:违反完整性约束:字段列表中的 1052 列“album_id”不明确

symfony 表单保存数据的问题

错误无法在laravel中工作并在数据库中给出列错误

完整性约束违规:1048 Column 'user_id' cannot be null 分配角色时发生错误(Laravel 5.3)