迁移失败我已经更改了格式但仍然失败
migrate failed i've changed the format and still failed
我正在尝试迁移,但由于此错误而失败,我已将 album_id/image_id 的格式更改为整数,但它一直出现此错误
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1005 Can't create table `moses_photography`.`images` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `images` add constraint `images_album_id_foreign` foreign key (`album_id`) references `albums` (`image_id`))
at C:\xampp\htdocs\moses_photography\vendor\laravel\framework\src\Illuminate\Database\Connection.php:716
712▕ // If an exception occurs when attempting to run a query, we'll format the error
713▕ // message to include the bindings with SQL, which will make this exception a
714▕ // lot more helpful to the developer instead of just the database's errors.
715▕ catch (Exception $e) {
➜ 716▕ throw new QueryException(
717▕ $query, $this->prepareBindings($bindings), $e
718▕ );
719▕ }
720▕ }
1 C:\xampp\htdocs\moses_photography\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `moses_photography`.`images` (errno: 150 "Foreign key constraint is incorrectly formed")")
2 C:\xampp\htdocs\moses_photography\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
PDOStatement::execute()
专辑table迁移
*/
public function up()
{
Schema::create('albums', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('category_id');
$table->integer('image_id');
$table->timestamps();
});
}
图片table迁移
Schema::create('images', function (Blueprint $table) {
$table->id();
$table->integer('album_id')->unsigned();
$table->foreign('album_id')->references('image_id')->on('albums');
$table->string('filename');
$table->timestamps();
});
在SQL
中的外键,需要与引用的id具有相同的类型。 Laravel 迁移方法 ->id()
,不是 unsignedInteger()
,而是 unsignedBigInteger()
。因此,以下更改应该对您有所帮助。
$table->unsignedBigInteger('album_id');
$table->foreign('album_id')->references('image_id')->on('albums');
我正在尝试迁移,但由于此错误而失败,我已将 album_id/image_id 的格式更改为整数,但它一直出现此错误
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1005 Can't create table `moses_photography`.`images` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `images` add constraint `images_album_id_foreign` foreign key (`album_id`) references `albums` (`image_id`))
at C:\xampp\htdocs\moses_photography\vendor\laravel\framework\src\Illuminate\Database\Connection.php:716
712▕ // If an exception occurs when attempting to run a query, we'll format the error
713▕ // message to include the bindings with SQL, which will make this exception a
714▕ // lot more helpful to the developer instead of just the database's errors.
715▕ catch (Exception $e) {
➜ 716▕ throw new QueryException(
717▕ $query, $this->prepareBindings($bindings), $e
718▕ );
719▕ }
720▕ }
1 C:\xampp\htdocs\moses_photography\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `moses_photography`.`images` (errno: 150 "Foreign key constraint is incorrectly formed")")
2 C:\xampp\htdocs\moses_photography\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
PDOStatement::execute()
专辑table迁移
*/
public function up()
{
Schema::create('albums', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('category_id');
$table->integer('image_id');
$table->timestamps();
});
}
图片table迁移
Schema::create('images', function (Blueprint $table) {
$table->id();
$table->integer('album_id')->unsigned();
$table->foreign('album_id')->references('image_id')->on('albums');
$table->string('filename');
$table->timestamps();
});
在SQL
中的外键,需要与引用的id具有相同的类型。 Laravel 迁移方法 ->id()
,不是 unsignedInteger()
,而是 unsignedBigInteger()
。因此,以下更改应该对您有所帮助。
$table->unsignedBigInteger('album_id');
$table->foreign('album_id')->references('image_id')->on('albums');