sqlstate[23000]:完整性约束冲突:1048列“univ”不能为null

c86crjj0  于 2021-06-18  发布在  Mysql
关注(0)|答案(3)|浏览(429)

我试图使用laravel和mysql在表中插入数据,但我看到了错误 SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'univ' cannot be null ,而我没有插入任何空值。我正在填表。原因是什么?请帮忙。
我的表单是education.blade.php,并给出了它的代码。

@section('content')

    <div class="container"><br>
        <h1 class="text-success text-center">Add Education Here</h1><br>
        <div class="col-md-offset-3 col-md-6 m-auto d-block">
            <form action="edu" method="post">
                <input type="hidden" name="_token" value="{{csrf_token()}}">
                <input type="text" name="degree" value=" MSc / BS" disabled>
                <div>
                    <div class="form-group">
                        <label>University: </label>
                        <input type="text" name="univ" id="" class="form-control">
                    </div>

                    <div class="form-group">
                        <label>Country: </label>
                        <input type="text" name="country" id="" class="form-control">
                    </div>

                    <div class="form-group">
                        <label>Year: </label>
                        <input type="number" name="year" id="" class="form-control">
                    </div>

                    <div class="form-group">
                        <label>Research Area: </label>
                        <input type="text" name="research" id="" class="form-control">
                    </div>
                </div>
                <input type="submit" name="submit" value="ADD!" class="btn btn-lg col-md-offset-3 col-md-6 m-auto d-block">
            </form>
        </div>
    </div>

@endsection

这里给出了我的迁移代码。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateEducsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('educs', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('registrations');
            $table->string('degree');
            $table->string('univ');
            $table->string('country');
            $table->integer('year');
            $table->text('research_area');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('educs');
    }
}

我的控制器的代码是educontoller.php

<?php

namespace App\Http\Controllers;

use Request;

use App\educ;

class EduController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('education');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        educ::create(Request::all());
        return 'inserted';
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

这里给出了我的模型的代码。

<?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class educ extends Model
    {
        //
        protected $fillable = ['user_id','degree','univ','country','year','research_area'];
    }

并且给出了我在提交表单时遇到的错误。
完整性约束冲突:1048列“univ”不能为null

y53ybaqx

y53ybaqx1#

我不知道到底是什么 educ::create(Request::all()); 是的,但是你能试着用下面的几行来改变这一行吗?告诉我们你仍然得到同样的错误。还有一点需要注意的是,您仍然需要在 univ 即使你不发送它,因为如果你不发送它,它被视为 null ```
$education = new educ($request->all());
$education->save();

mm5n2pyu

mm5n2pyu2#

你需要像m.a.solano93说的那样使univ列为空。最简单的方法如下。

php artisan make:migration update_educs_table

在迁移过程中 up 方法

Schema::table('educs', function (Blueprint $table) {
    $table->string('univ')->nullable()->change();
});

然后运行迁移

php artisan migrate
hivapdat

hivapdat3#

你的问题在于univ列是空的。在为该表编写初始迁移时,没有添加 ->nullable() 列修饰符。因此,由orm生成的创建表的sql没有 NULL 你会想到的。根据您的验收标准,这里有两个选项:1。使用迁移2添加可为null的修饰符。确保每次发帖时都提交univ,从而使它成为一个好用户体验的前端必填字段。
这份清单并不详尽,但它会让你走的。希望一切顺利!

相关问题