【问题标题】:Resolving "Integrity constraint violation: 19 NOT NULL", what's the most proper solution?解决“违反完整性约束:19 NOT NULL”,最合适的解决方案是什么?
【发布时间】:2020-08-14 06:39:09
【问题描述】:

试图编写一个函数来在我的配置文件表中创建一个新的“配置文件”并得到以下错误:

“SQLSTATE[23000]:完整性约束违规:19 NOT NULL 约束失败:profiles.about(SQL:插入“profiles”(“dateofbirth”、“state”、“zipcode”、“profilepic”、“user_id” , "updated_at", "created_at") 值 (2020-04-15, FL, 12345, /tmp/phpTT6CZr, 1, 2020-04-30 00:48:23, 2020-04-30 00:48:23) )"

在过去的几个小时里,我一直在阅读类似问题的答案。尝试了几种不同的东西,到目前为止没有运气。希望看到一个适用于我的代码的解决方案,并更好地了解错误的确切开始位置。该错误消息使我相信这与表中的“关于”部分有关。但不确定。我认为向控制器添加“protected $guarded = [];”可以解决,但结果相同。

这是我的工作:

迁移文件:

public function up()
{
    Schema::create('profiles', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('user_id'); //foreign key
        $table->text('about')->nullable;
        $table->text('profilepic')->nullable;
        $table->date('dateofbirth')->nullable;
        $table->unsignedinteger('zipcode')->nullable;
        $table->string('state')->nullable;
        $table->timestamps();

        $table->index('user_id'); //index for foreign key
    });
}

个人资料模型:

class profile extends Model {


protected $guarded = [];


public function user()
{
    return $this->belongsTo(User::class);
} }

我也尝试过如下更改配置文件模型:

class profile extends Model {

public function user()
{
    return $this->belongsTo(User::class);
}

/**
 * The attributes that should be cast to native types.
 *
 * @var array
*/

protected $casts = [
    'dateofbirth' => 'datetime',
    'zipcode' => 'unsignedinteger'
];

 /*
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'about','profilepic','state', 'user_id', 'updated_at', 'created_at'
]; }

它们都提供相同的错误消息,但数组值略有不同

这是我的控制器存储功能:

public function store()
{
    $data = request()->validate([
        'dateofbirth' => 'required',
        'state' => 'required',
        'zipcode' => 'required',
        'profilepic' => 'image'
    ]); 

    auth()->user()->profile()->create($data);

    dd(request()->all());
}

这里是视图:

@extends('layouts.app')

@push('styles')
<link href="{{ asset('css/profile.css') }}" rel="stylesheet">
@endpush

@section('content')
{{-- This needs to present a create profile form --}}

<div class="row">
    <h1 class="pl-4">CREATE YOUR PROFILE</h1>
</div>

<form action="/profile" class="pl-4" enctype="multipart/form-data" method="post">
    @csrf

    <div class="form-group row">
        <label for="profilepic"
        class="col-md-4 ocl-form-label"
        >Upload a Profile Picture</label>

        <input type="file"
        class="form-control-file"
        id="profilepic"
        name="profilepic">
    </div>

    <div class="form-group">
        <label for="about">Write your "About" Section here. What do you want us to know about you?</label>
        <textarea type="text" class="form-control" id="about" name="about" rows="3"></textarea>
    </div>

    <div class="form-group">
        <label for="dateofbirth">Date of Birth</label>

        <input type="date"
        id="dateofbirth"
        name="dateofbirth">
    </div>

    <div class="form-group">
        <label for="zipcode">Zipcode</label>
        <input type="text" id="zipcode" name="zipcode">
    </div>

    <div class="form-group">
        <label for="State">State</label>
        <input type="text" id="state" name="state">
    </div>

    <div class="form-group row pt-4">
        <button class="btn btn-primary">Submit</button>
    </div>
</form> @endsection

【问题讨论】:

    标签: php sql laravel nullable mass-assignment


    【解决方案1】:

    该错误意味着您正在尝试将外键列设置为 null,这是不可接受的,在这种情况下,profiles 表上的 user_id。尝试修改您的代码: 在您的配置文件模型中,添加质量分配列:

    protected $fillable = ['dateofbirth', 'state', 'zipcode', 'profilepic'];
    

    在您的控制器存储方法中:

    //assuming the route method is authenticated such that there's always a logged in user
    $user = auth()->user();
    $data['user_id'] = $user->id;
    $profile = Profile::create($data);
    

    【讨论】:

    • 这不起作用。不知道为什么。我可以看到为什么 user_id 可能会导致问题(尽管我的印象是它会被自动抓取 //in user class public function profile() { return $this->hasOne(profile::class); } //在配置文件类中 public function user() { return $this->belongsTo(User::class); }
    • 是抛出同样的完整性约束错误还是其他错误?
    • 错误似乎来自这里$table-&gt;text('about')-&gt;nullable;。你能修改你的代码以指定about 列,可能是一个空字符串吗?
    • 啊,是的...这行得通,数据从表单到“关于”似乎有些不对劲。在控制器中添加了一个随机字符串,如 $data['about'] = " asdsafdg";它奏效了。
    • 好!要使 null 属性在 about 列上起作用,您可以将其更改为字符串 (varchar) 类型或始终向其发送(至少是一个空的)字符串
    【解决方案2】:

    我还要补充一下,我已经通过实施@djunehor 的回答解决了这个问题。但是有助于解决问题的一件事是将其添加到控制器中:

    public function store(Request $request)
    

    起初我并没有将请求传入并将其保存到这样的变量中,但是这一步似乎对我遇到的错误产生了很大的影响。

    起初我只是这样做:

    public function store()
    

    【讨论】:

      猜你喜欢
      • 2020-10-02
      • 2018-03-20
      • 2021-06-10
      • 2021-03-24
      • 1970-01-01
      • 2017-03-01
      • 2019-07-27
      • 2020-01-28
      • 2014-11-27
      相关资源
      最近更新 更多