Laravel 7 错误消息:'SQLSTATE[23000]:违反完整性约束:19 NOT NULL 约束失败:profiles.url
Laravel 7 Error message: 'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: profiles.url
更新:问题已解决!
我正在刷新我的 Laravel 技能(使用版本 7.13.0),按照 freecodecamp.org 教程,此处:https://www.youtube.com/watch?v=ImtZ5yENzgE&t=11889s(1:21:49 到 1:22:50).当我使用 php artisan tinker 手动添加配置文件时,就像我在前端所做的那样,它无法保存。数据库是sqlite。
这是完整的错误:
Illuminate/Database/QueryException with message 'SQLSTATE[23000]:
Integrity constraint violation: 19 NOT NULL constraint failed:
profiles.url (SQL: insert into "profiles" ("title", "description",
"user_id", "updated_at", "created_at") values (Cool Title,
Description, 1, 2020-05-29 18:41:02, 2020-05-29 18:41:02))'
我在 profiles_table.php 文件夹 database\migrations 中的功能似乎没问题。是这样的:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('title')->nullable();
$table->text('description')->nullable();
$table->string('url')->nullable();
$table->timestamps();
$table->index('user_id');
});
}
Profile.php模型函数是:
public function user()
{
return $this->belongsTo(User::class);
}
而User.php模型函数是:
public function profile()
{
return $this->hasOne(Profile::class);
}
所以,在终端里,输入php artisan tinker后,我在保存失败前填写了以下内容:
>>> $profile = new \App\Profile();
=> App\Profile {#3056}
>>> $profile->title = 'Cool Title';
=> "Cool Title"
>>> $profile->description = 'Description';
=> "Description"
>>> $profile->user_id = 1;
=> 1
>>> $profile->save();
编辑:这是控制器文件ProfilesController.php,如果它有助于解决问题:
<?php
namespace App\Http\Controllers;
use \App\User;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function index($user)
{
$user = User::find($user);
return view('home', [
'user' => $user,
]);
}
}
所有部分都正常,在我输入保存后 - 它显示了上面的错误。因此我无法继续显示手动创建的新配置文件。
我搜索了 Google 并在此处寻找答案,例如 this question and 。它没有解决我的问题,我认为其他问题也不那么相关。
我应该怎么做才能修复它?
解决方案
感谢@mrhn,添加一个新的 null table + 按照此处的建议安装 composer require doctrine/dbal
:.
问题是您的数据库不允许列 url
为空。您的代码很好,没有任何问题,看来您可能 运行 之后迁移并更改了列定义。
在 Laravel 中仅迁移 运行 一次并确保数据库在不同系统中具有相同的结构。为了让字段可以为空进行新的迁移。
php artisan make:migration url_nullable
对于您的新迁移文件,添加以下迁移。这只是告诉迁移将 url
更新为 string
和 nullable
.
Schema::table('profiles', function (Blueprint $table) {
$table->string('url')->nullable()->change();
});
之后 运行 您的迁移和数据库应该是最新的。
php artisan migrate
更新:问题已解决!
我正在刷新我的 Laravel 技能(使用版本 7.13.0),按照 freecodecamp.org 教程,此处:https://www.youtube.com/watch?v=ImtZ5yENzgE&t=11889s(1:21:49 到 1:22:50).当我使用 php artisan tinker 手动添加配置文件时,就像我在前端所做的那样,它无法保存。数据库是sqlite。
这是完整的错误:
Illuminate/Database/QueryException with message 'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: profiles.url (SQL: insert into "profiles" ("title", "description", "user_id", "updated_at", "created_at") values (Cool Title, Description, 1, 2020-05-29 18:41:02, 2020-05-29 18:41:02))'
我在 profiles_table.php 文件夹 database\migrations 中的功能似乎没问题。是这样的:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->string('title')->nullable();
$table->text('description')->nullable();
$table->string('url')->nullable();
$table->timestamps();
$table->index('user_id');
});
}
Profile.php模型函数是:
public function user()
{
return $this->belongsTo(User::class);
}
而User.php模型函数是:
public function profile()
{
return $this->hasOne(Profile::class);
}
所以,在终端里,输入php artisan tinker后,我在保存失败前填写了以下内容:
>>> $profile = new \App\Profile();
=> App\Profile {#3056}
>>> $profile->title = 'Cool Title';
=> "Cool Title"
>>> $profile->description = 'Description';
=> "Description"
>>> $profile->user_id = 1;
=> 1
>>> $profile->save();
编辑:这是控制器文件ProfilesController.php,如果它有助于解决问题:
<?php
namespace App\Http\Controllers;
use \App\User;
use Illuminate\Http\Request;
class ProfilesController extends Controller
{
public function index($user)
{
$user = User::find($user);
return view('home', [
'user' => $user,
]);
}
}
所有部分都正常,在我输入保存后 - 它显示了上面的错误。因此我无法继续显示手动创建的新配置文件。
我搜索了 Google 并在此处寻找答案,例如 this question and
我应该怎么做才能修复它?
解决方案
感谢@mrhn,添加一个新的 null table + 按照此处的建议安装 composer require doctrine/dbal
:.
问题是您的数据库不允许列 url
为空。您的代码很好,没有任何问题,看来您可能 运行 之后迁移并更改了列定义。
在 Laravel 中仅迁移 运行 一次并确保数据库在不同系统中具有相同的结构。为了让字段可以为空进行新的迁移。
php artisan make:migration url_nullable
对于您的新迁移文件,添加以下迁移。这只是告诉迁移将 url
更新为 string
和 nullable
.
Schema::table('profiles', function (Blueprint $table) {
$table->string('url')->nullable()->change();
});
之后 运行 您的迁移和数据库应该是最新的。
php artisan migrate