SQLSTATE[23000]:违反完整性约束:1452 无法在 Laravel9 中添加或更新子行
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row in Laravel9
我在 Laravel 9 中 运行 命令 php artisan db:seed
时违反了完整性约束。
错误
PDOException::("SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (hunger-db
.restaurants
, CONSTRAINT restaurants_user_id_foreign
FOREIGN KEY (user_id
) REFERENCES users
(id
) ON DELETE CASCADE)")
setup_users_table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('phone');
$table->tinyInteger('role_as')->default('0');
$table->tinyInteger('gender')->default('0');
$table->string('date_of_birth')->default('1999/9/9');
$table->rememberToken();
$table->timestamps();
});
}
setum_restaurants_table
public function up()
{
Schema::create('restaurants', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('img_src');
$table->string('description');
$table->string('minimum_order_amount');
$table->string('working_hours_open');
$table->string('working_hours_close');
$table->string('delivery_time');
$table->string('delivery_fee');
$table->boolean('status')->default('0');
$table->unsignedBigInteger('user_id')->default('1');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
用户模型
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
protected $fillable = [
'name',
'email',
'password',
'phone',
];
public function orders()
{
return $this->hasMany(Order::class);
}
public function restaurants()
{
return $this->belongsTo(Restaurant::class);
}
}
餐厅模特
class Restaurant extends Model
{
use HasFactory;
protected $fillable = [
'name',
'img_src',
'description',
];
public function users()
{
return $this->belongsTo(User::class);
}
}
查看您添加到餐厅table的user_id。确保它存在于 Users table 中。可能是我的英文不足以告诉你,可以再找一个answer找问题。
我看到这里有些东西需要改变
setup_restaurants_table
我看不太清楚 user_id
默认值为 1,也许最好在创建餐厅时定义值或手动将其分配给用户 1。
Schema::create('restaurants', function (Blueprint $table) {
$table->id();
...
$table->foreignId('user_id')->constrained()->onDelete('CASCADE');
});
数据库更改后运行 php artisan migrate:fresh
User.php
应该改变与 hasMany
餐厅的关系
public function restaurants()
{
return $this->hasMany(Restaurant::class);
}
播种
还要确保您的数据库播种正确,我的意思是先创建用户,然后再创建餐厅,使用现有值设置 user_id
。
也许创建这样的餐厅:
$user = User::factory()
->hasRestaurants(3)
->create();
如有工厂变更运行php artisan db:seed
我在 Laravel 9 中 运行 命令 php artisan db:seed
时违反了完整性约束。
错误
PDOException::("SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
hunger-db
.restaurants
, CONSTRAINTrestaurants_user_id_foreign
FOREIGN KEY (user_id
) REFERENCESusers
(id
) ON DELETE CASCADE)")
setup_users_table
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('phone');
$table->tinyInteger('role_as')->default('0');
$table->tinyInteger('gender')->default('0');
$table->string('date_of_birth')->default('1999/9/9');
$table->rememberToken();
$table->timestamps();
});
}
setum_restaurants_table
public function up()
{
Schema::create('restaurants', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('img_src');
$table->string('description');
$table->string('minimum_order_amount');
$table->string('working_hours_open');
$table->string('working_hours_close');
$table->string('delivery_time');
$table->string('delivery_fee');
$table->boolean('status')->default('0');
$table->unsignedBigInteger('user_id')->default('1');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
用户模型
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
protected $fillable = [
'name',
'email',
'password',
'phone',
];
public function orders()
{
return $this->hasMany(Order::class);
}
public function restaurants()
{
return $this->belongsTo(Restaurant::class);
}
}
餐厅模特
class Restaurant extends Model
{
use HasFactory;
protected $fillable = [
'name',
'img_src',
'description',
];
public function users()
{
return $this->belongsTo(User::class);
}
}
查看您添加到餐厅table的user_id。确保它存在于 Users table 中。可能是我的英文不足以告诉你,可以再找一个answer找问题。
我看到这里有些东西需要改变
setup_restaurants_table
我看不太清楚 user_id
默认值为 1,也许最好在创建餐厅时定义值或手动将其分配给用户 1。
Schema::create('restaurants', function (Blueprint $table) {
$table->id();
...
$table->foreignId('user_id')->constrained()->onDelete('CASCADE');
});
数据库更改后运行 php artisan migrate:fresh
User.php
应该改变与 hasMany
餐厅的关系
public function restaurants()
{
return $this->hasMany(Restaurant::class);
}
播种
还要确保您的数据库播种正确,我的意思是先创建用户,然后再创建餐厅,使用现有值设置 user_id
。
也许创建这样的餐厅:
$user = User::factory()
->hasRestaurants(3)
->create();
如有工厂变更运行php artisan db:seed