【问题标题】:Laravel 7 Error message: 'SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: profiles.urlLaravel 7 错误消息:'SQLSTATE [23000]:完整性约束违规:19 NOT NULL 约束失败:profiles.url
【发布时间】:2020-09-17 12:22:48
【问题描述】:

更新:问题已解决!

我正在更新我的 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。

这是完整的错误:

使用消息“SQLSTATE[23000] 照亮/数据库/查询异常: 完整性约束违规:19 NOT NULL 约束失败: profile.url (SQL: 插入 "profiles" ("title", "description", “user_id”、“updated_at”、“created_at”)值(酷标题, 说明, 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,
        ]);
    }
}

所有部分都正常,在我输入 SAVE 后 - 它显示了上面的错误。因此我无法继续显示手动创建的新配置文件。

我在 Google 上搜索并在此处查找答案,例如 this questionthat question。它没有解决我的问题,而且我认为其他问题不那么相关。

我应该怎么做才能修复它?

解决方案

感谢@mrhn,添加一个新的空表+安装composer require doctrine/dbal,如下所示:https://stackoverflow.com/a/37533819/12952748

【问题讨论】:

  • 退出tinker,再次迁移数据库,再次进入tinker重试
  • 值得注意的是,您的代码没有问题,我无法重现错误复制面食
  • 尝试将 URL 添加到模型中的可填充数组中。
  • 萨利姆,我做了退出修补程序并再次进入,它没有改变。但是你是什么意思“代码没有问题”?在哪里展示东西? @TahaPaksu 在哪里添加 URL?在配置文件控制器返回?查一下,我加进去了。

标签: php mysql laravel sqlite tinker


【解决方案1】:

问题是您的数据库不允许列url 为空。您的代码很好,没有任何问题,您似乎已经运行了迁移并更改了列定义。

在 Laravel 中,迁移只运行一次,并确保数据库在系统之间具有相同的结构。为了使该字段可以为空,请进行新的迁移。

php artisan make:migration url_nullable

为您的新迁移文件添加以下迁移。这只是告诉迁移将url 更新为stringnullable

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

然后运行您的迁移,您的数据库应该是最新的。

php artisan migrate

【讨论】:

  • 我按照您的建议添加了一个新的可为空表,但现在在终端中键入“php artisan migrate”后出现问题 - 因为现有的配置文件表。当如果完成迁移说:SQLSTATE [HY000]:一般错误:1表“profiles”已经存在(SQL:创建表“profiles”(“id”整数不为空主键自动增量,“user_id”整数不为空, "title" varchar null, "description" text null, "url" varchar null, "created_at" datetime null, "updated_at" datetime null)))
  • 你需要做 Schema::table('profiles') 你可能做了 Schema::create('profiles');
  • 知道了,谢谢两位!它现在可以工作了,也安装了:composer 需要学说/dbal。我首先遇到了一个错误,建议按照这里的建议安装它:stackoverflow.com/a/37533819/12952748
猜你喜欢
  • 2020-01-28
  • 2018-03-20
  • 2021-06-10
  • 2021-03-24
  • 1970-01-01
  • 2020-12-28
  • 2021-01-26
  • 1970-01-01
  • 2022-11-02
相关资源
最近更新 更多