【问题标题】:Laravel 5: SQLSTATE[23000]: Integrity constraint violationLaravel 5:SQLSTATE[23000]:违反完整性约束
【发布时间】:2015-07-17 00:55:47
【问题描述】:

我有一个包含以下字段的表单:姓名、全名、描述、电子邮件、密码、州、城市(选择州时通过 Ajax 填写)和照片(图片上传)。 这是我的架构:

public function up()
{
    Schema::create('states', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('acronym');
        $table->string('name');
    });

    Schema::create('cities', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->integer('state_id')->unsigned();
        $table->foreign('state_id')->references('id')->on('states');
    });

    Schema::create('users', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('name');
        $table->string('fullname');
        $table->string('photo');
        $table->text('description');
        $table->string('email');
        $table->string('password', 60);
        $table->integer('city_id')->unsigned();
        $table->foreign('city_id')->references('id')->on('cities');
        $table->rememberToken();
    });
}

我的问题是,当我按下提交按钮时,出现以下错误信息:

SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(yearbook.users,CONSTRAINT users_city_id_foreign FOREIGN KEY (city_id) REFERENCES cities (id)) (SQL: 插入users (name, description, email) 值(Gabriel, Description, email@gmail.com))

所以问题与 city_id 作为外键有关(表单选项中的“值”属性是 id)。来例:

<option value="281">Abaíra</option>

但是当我尝试通过 phpmyadmin 插入时,它插入时没有错误。

那么可能是什么问题?

这是我在控制器上的保存方法:

public function saveRegister()
{
    $rules = array(
        'name' => 'required',
        'description' => 'required',
        'fullname' => 'required',
        'email' => 'required',
        'password' => 'required',
        'state' => 'required',
        'city' => 'required',
        'photo' => 'required',
        'photo' => 'image|max:3000000',
    );
    $val = \Validator::make(\Input::all(), $rules);

    if($validar->fails()){
        return redirect('register')->withErrors($val);
    } else {
        $user = new User(array(
            'name' => \Input::get('name'),
            'description' => \Input::get('description'),
            'fullname' => \Input::get('fullname'),
            'email' => \Input::get('email'),
            'city_id' => \Input::get('city'),
        ));
        $user->timestamps = false;
        $user->save();
        return redirect('register')->with('message', 'User saved!');

    }
}

编辑:在 saveRegister() 方法中修复 city_id。

【问题讨论】:

  • 您在users 表中有city_id,但在您的代码中,您使用的是city
  • 你是对的,但我修复了,但我仍然得到错误。

标签: php mysql validation constraints laravel-5


【解决方案1】:

而不是

$user = new User(array(
        'name' => \Input::get('name'),
        'description' => \Input::get('description'),
        'fullname' => \Input::get('fullname'),
        'email' => \Input::get('email'),
        'city' => \Input::get('city'),
    ));

这样做

$user = new User();
$user->fill(array(
        'name' => \Input::get('name'),
        'description' => \Input::get('description'),
        'email' => \Input::get('email')
    ));
$user->fullname = \Input::get('fullname');
$user->city= \Input::get('city');

或更改模型中的$fillable 属性

【讨论】:

  • 谢谢。问题在于 $fillable 属性:它没有 city_id。
  • 总是乐于提供帮助!
  • 那个通病,在fillable中缺少值。在我的情况下,我在可填充数组上使用 '' 但使用 fk 我不得不放置 '', 'fk_id'
【解决方案2】:

这可能是因为您的 city.id 字段未签名,而您的 FK 字段未签名。关系中使用的所有列必须完全匹配。

尝试将您的代码更改为:

Schema::create('cities', function(Blueprint $table)
{
    $table->increments('id')->unsigned();

【讨论】:

  • 那么试试这样:
  • $table-&gt;unsignedInteger('id', true);
  • 澄清一下,您需要删除现有表并重新运行迁移才能使此更改生效。
  • 我知道。我删除了整个数据库并再次执行此操作,但得到了相同的结果。
  • 我会试试 unsignedInteger。
猜你喜欢
  • 2017-09-11
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
  • 2014-08-18
  • 2015-03-19
  • 2015-11-27
  • 2015-01-20
相关资源
最近更新 更多