外键约束在laravel 7中的格式不正确
我正在使用laravel构建一个酒店管理应用程序。我试图在laravel中创建表'reservations',但是当我运行'migrate:fresh'命令时,我得到以下结果:错误“外键约束格式不正确”。有人能说出你说的这个错误是什么意思吗 错误消息外键约束在laravel 7中的格式不正确,laravel,Laravel,我正在使用laravel构建一个酒店管理应用程序。我试图在laravel中创建表'reservations',但是当我运行'migrate:fresh'命令时,我得到以下结果:错误“外键约束格式不正确”。有人能说出你说的这个错误是什么意思吗 错误消息 SQLSTATE[HY000]: General error: 1005 Can't create table `hotelplex`.`reservations` (errno: 150 "Foreign key constraint is
SQLSTATE[HY000]: General error: 1005 Can't create table `hotelplex`.`reservations` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `reservations` add constraint `reservations_reservations_user_id_foreign` foreign key (`reservations_user_id`) references `users` (`id`) on delete cascade)
at C:\xampp\htdocs\hotelplex\vendor\laravel\framework\src\Illuminate\Database\Connection.php:669
665| // If an exception occurs when attempting to run a query, we'll format the error
666| // message to include the bindings with SQL, which will make this exception a
667| // lot more helpful to the developer instead of just the database's errors.
668| catch (Exception $e) {
> 669| throw new QueryException(
670| $query, $this->prepareBindings($bindings), $e
671| );
672| }
673|
1 C:\xampp\htdocs\hotelplex\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463
PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `hotelplex`.`reservations` (errno: 150 "Foreign key constraint is incorrectly formed")")
2 C:\xampp\htdocs\hotelplex\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463
PDOStatement::execute()
$table->unsignedInteger('user_id');
$table->unsignedInteger('room_type_id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('room_type_id');
->onDelete('cascade')代码>应位于结构创建部分
更改为:
public function up()
{
Schema::create('reservations', function (Blueprint $table) {
// Structure
$table->id();
$table->integer('uid')->unique();
$table->timestamp('date');
$table-> unsignedBigInteger('user_id')->onDelete('cascade');
$table-> unsignedBigInteger('room_type_id')->onDelete('cascade');
$table->integer('adults')->default(1);
$table->integer('kids')->default(0);
$table->date('check_in');
$table->date('check_out');
$table->integer('number_of_room')->default(1);
$table->enum('status',['PENDING','CANCEL','SUCCESS'])->default('PENDING');
$table->timestamps();
// Relationships
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('room_type_id')->references('id')->on('room_types');
});
}
问题似乎是用户id和房间id的数据类型不匹配
$table->id();$table->bigIncrements('id')别名的别名
因此,您需要有关预订的“外国用户id
和房间类型\u id
列,如:
public function up()
{
Schema::create('reservations', function (Blueprint $table) {
// Structure
$table->id();
...
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('room_type_id');
...
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('room_type_id')->references('id')->on('room_types')->onDelete('cascade');
...
})
}
Laravel6
Laravel6
没有任何方法id()
在表中创建id
<代码>Laravel7
不执行。。我尝试使用Laravel6
创建id
并得到以下错误
似乎您发布了错误的错误,或者您已经在表中手动创建了id
您可以使用大增量
,大整数
,增量
,整数
等
您可以找到所有可用的方法
Laravel7
根据Laravel7
$table->id()
是$table->bigIncrements('id')
的别名,即无符号大整数
要创建外键
,子列
的数据类型必须与父列
完全匹配
由于users.id
和room\u-types.id
是一个大增量
,那么reservations.user\u-id
和reservations.room\u-type\u-id
也需要是一个无符号大整数
,而不是无符号整数
所以要让它发挥作用
改变
SQLSTATE[HY000]: General error: 1005 Can't create table `hotelplex`.`reservations` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `reservations` add constraint `reservations_reservations_user_id_foreign` foreign key (`reservations_user_id`) references `users` (`id`) on delete cascade)
at C:\xampp\htdocs\hotelplex\vendor\laravel\framework\src\Illuminate\Database\Connection.php:669
665| // If an exception occurs when attempting to run a query, we'll format the error
666| // message to include the bindings with SQL, which will make this exception a
667| // lot more helpful to the developer instead of just the database's errors.
668| catch (Exception $e) {
> 669| throw new QueryException(
670| $query, $this->prepareBindings($bindings), $e
671| );
672| }
673|
1 C:\xampp\htdocs\hotelplex\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463
PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `hotelplex`.`reservations` (errno: 150 "Foreign key constraint is incorrectly formed")")
2 C:\xampp\htdocs\hotelplex\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463
PDOStatement::execute()
$table->unsignedInteger('user_id');
$table->unsignedInteger('room_type_id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('room_type_id');
至
SQLSTATE[HY000]: General error: 1005 Can't create table `hotelplex`.`reservations` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `reservations` add constraint `reservations_reservations_user_id_foreign` foreign key (`reservations_user_id`) references `users` (`id`) on delete cascade)
at C:\xampp\htdocs\hotelplex\vendor\laravel\framework\src\Illuminate\Database\Connection.php:669
665| // If an exception occurs when attempting to run a query, we'll format the error
666| // message to include the bindings with SQL, which will make this exception a
667| // lot more helpful to the developer instead of just the database's errors.
668| catch (Exception $e) {
> 669| throw new QueryException(
670| $query, $this->prepareBindings($bindings), $e
671| );
672| }
673|
1 C:\xampp\htdocs\hotelplex\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463
PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `hotelplex`.`reservations` (errno: 150 "Foreign key constraint is incorrectly formed")")
2 C:\xampp\htdocs\hotelplex\vendor\laravel\framework\src\Illuminate\Database\Connection.php:463
PDOStatement::execute()
$table->unsignedInteger('user_id');
$table->unsignedInteger('room_type_id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('room_type_id');
像这样:
public function up()
{
Schema::create('reservations', function (Blueprint $table) {
$table->id();
$table->integer('uid')->unique();
$table->timestamp('date');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('room_type_id');
$table->integer('adults')->default(1);
$table->integer('kids')->default(0);
$table->date('check_in');
$table->date('check_out');
$table->integer('number_of_room')->default(1);
$table->enum('status',['PENDING','CANCEL','SUCCESS'])->default('PENDING');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('room_type_id')->references('id')->on('room_types')->onDelete('cascade');
});
}
在laravel 8上,您可以使用
$table->id();
....
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('room_type_id');
...
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('restrict');
$table->foreign('room_type_id')->references('id')->on('room_types')->onDelete('restrict');
根据截至6日它所做的文档
$table->id();$table->bigIncrements('id')的别名
$table->id();在拉雷维尔有售6@jeremykenedy它在Laravel6中不可用,但在Laravel7@HarshithVA它在Laravel6中不可用,在Laravel7中可用–我真的很抱歉你是对的,它在Laravel7中可用。