gpt4 book ai didi

php - Laravel 5.4 将带有外键的列添加到现有表错误

转载 作者:行者123 更新时间:2023-11-30 23:44:53 25 4
gpt4 key购买 nike

我尝试创建以下迁移文件来更新我的用户表:

<?php

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

class AddSedeIdToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->integer('sede_id')->unsigned();
$table->foreign('sede_id')->references('id')->on('sedes');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}

这是我的 sedes 迁移文件,我在表中植入了 2 个测试行

<?php

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

class CreateSedesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sedes', function (Blueprint $table)
{
$table->increments('id');
$table->string('name')->nullable();
});
}

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

还有我的 user.php 模型

<?php

namespace App;

use App\Cuota;
use App\ticketsIncidente;
use App\ticketsMessage;
use App\Sancion;
use App\Reserva;
use App\Espacio;
use App\Sede;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
use Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'role', 'facebook_id'
];

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];


public function user(){
$this->hasOne('App\Sede');
$this->hasMany('App\Cuota');
$this->hasMany('App\Reserva');
$this->hasMany('App\Sancion');
$this->hasMany('App\ticketIncidente');
$this->hasMany('App\ticketMessage');
return;
}
}

Sede.php 类

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\User;

class Sede extends Model
{
protected $fillable = ['name'];

public function sede() {
$this->belongsTo('App\User');
return;
}
}

当我运行 php artisan migrate 时它返回

  [Illuminate\Database\QueryException]                                         
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update
a child row: a foreign key constraint fails (`clubgaston`.`#sql-446_b3`, CO
NSTRAINT `users_sede_id_foreign` FOREIGN KEY (`sede_id`) REFERENCES `sedes`
(`id`)) (SQL: alter table `users` add constraint `users_sede_id_foreign` f
oreign key (`sede_id`) references `sedes` (`id`))



[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update
a child row: a foreign key constraint fails (`clubgaston`.`#sql-446_b3`, CO
NSTRAINT `users_sede_id_foreign` FOREIGN KEY (`sede_id`) REFERENCES `sedes`
(`id`))

提前致谢

最佳答案

不管运行了 alter table 错误还是在 users 表中创建了 sede_id 但没有 FOREIGN KEY 引用;所以我运行了另一个迁移,但没有再次定义列,只分配了外部,并且成功了。

  public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->foreign('sede_id')->references('id')->on('sedes');
});
}

https://laravel.com/docs/5.4/migrations#foreign-key-constraints

关于php - Laravel 5.4 将带有外键的列添加到现有表错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44318899/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com