【问题标题】:Laravel eloquent Cannot add or update a child row: a foreign key constraint failsLaravel eloquent 无法添加或更新子行:外键约束失败
【发布时间】:2017-06-28 00:08:52
【问题描述】:

你好,这是我的亲戚:

地点:

public function _geoloc()
{
    return $this->belongsTo('App\GeoLoc',  'id');
}

地质:

public function locations()
{
    return $this->hasOne('App\Location', 'geoloc_id');
}

控制器:

    $geoloc = new Geoloc;
    $geoloc->lat = $request->input('lat');
    $geoloc->lng = $request->input('lng');
    $geoloc->save();
    $geoloc_id = $geoloc->id;

    $location = new Location;
    $location->business_id = $business_id;
    $location->latitude = $request->input('lat');
    $location->longitude = $request->input('lng');
    $location->slug = $request->input('name');
    $location->geoloc_id = $geoloc_id;
    $location->save();

完整的错误信息:

SQLSTATE[23000]:违反完整性约束:1452 无法添加或 更新子行:外键约束失败 (liveoldham.locations, 约束locations_geoloc_id_foreign 外键 (geoloc_id) 引用 geoloc (id) 删除级联 ON UPDATE CASCADE) (SQL: 插入locations (business_id, latitude, longitude, slug, geoloc_id, updated_at, created_at) 值 (1, 53.4867546, -2.054066599999942, Live & Now, 94、2017-06-27 15:22:51、2017-06-27 15:22:51))

所有值都是正确的,行被插入到 geoloc 但它没有被插入到位置,这是我遇到的错误,如何解决?

//编辑:

<?php

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

class CreateLocationsTable extends Migration {

    public function up()
    {
        Schema::create('locations', function(Blueprint $table) {
            $table->increments('id');
            $table->integer('business_id')->unsigned();
            $table->float('latitude', 10,6);
            $table->float('longitude', 10,6);
            $table->integer('geoloc_id')->unsigned();
            $table->string('slug', 29)->nullable();
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('locations');
    }
}

第二个:

<?php

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

class CreateGeolocTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
            Schema::create('_geoloc', function(Blueprint $table) {
            $table->increments('id');
            $table->float('lat', 10,6);
            $table->float('lng', 10,6);
            $table->timestamps();
        });
    }

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

【问题讨论】:

    标签: laravel laravel-5 eloquent


    【解决方案1】:

    您应该在更新 belongsTo 关系时使用 associate 方法。 所以,而不是

    $location->geoloc_id = $geoloc_id;
    

    使用

    $location->_geoloc()->associate($geoloc);
    $location->save();
    

    还要确保您的 belongsTo 和 hasOne 有正确的 ForeignKey 作为第二个参数。

    根据您的迁移,

    你的位置模型应该有

    public function _geoloc()
    {
        return $this->belongsTo('App\GeoLoc',  'geoloc_id');
    }
    

    另外,我没有看到您使用迁移本身设置外键关系。因此,请将您的 CreateLocationsTable 更新为:

    $table->integer('geoloc_id')->unsigned();
    $table->foreign('geoloc_id')->references('id')->on('_geoloc')->onDelete('cascade');
    

    现在您应该重新运行迁移,它会起作用。

    【讨论】:

    • 我现在得到:SQLSTATE[HY000]:一般错误:1364 字段“geoloc_id”没有默认值(SQL:插入locationsbusiness_idlatitude,@ 987654329@, slug, id, updated_at, created_at) 值 (4, 53.4867546, -2.054066599999942, Live & Now, 97, 2017-06-27 15:46:32, 201-46:32, 201-6-6 15:46:32))
    • 插入查询正在寻找名为“id”的字段,而不是“geoloc_id”。应该是belongsTo('App\GeoLoc', 'geoloc_id');。我将不得不查看您的表结构以获取 geoloc 和位置。如果您发布这两个的迁移会更好。
    • 我这样做了,但没有用我得到了与问题中相同的错误
    猜你喜欢
    • 2017-09-14
    • 2021-09-21
    • 2020-07-29
    • 2020-10-04
    相关资源
    最近更新 更多