![](/img/trans.png)
[英]General error: 1005 Can't create table (errno: 150 "Foreign key constraint is incorrectly formed")
[英]General error : 1005 Can't create table ... (errno:150 "Foreign key constraint is incorrectly formed"
我必須在我的在線項目上運行遷移,但它不適用於外鍵。 我有兩個表:media 和 video_categorie(現有) 這是我創建 video_categorie 文件的遷移文件:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateVideoCategorieTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('video_categorie', function (Blueprint $table) {
$table->increments('id');
$table->string('nom_fr', 50);
$table->string('nom_en', 50)->nullable();
$table->unsignedSmallInteger('ordre')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('video_categorie');
}
}
另一個在我的媒體表上創建外鍵:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddForeignKeyToMediaTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('media', function (Blueprint $table) {
$table->unsignedInteger('video_categorie_id')->nullable();
$table->unsignedSmallInteger('ordre_video')->nullable();
$table->foreign('video_categorie_id')->references('id')->on('video_categorie');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('media', function (Blueprint $table) {
$table->dropForeign('media_video_categorie_id_foreign');
$table->dropColumn('video_categorie_id');
$table->dropColumn('ordre_video');
});
}
}
當我在本地服務器上嘗試時,它就像一個魅力,我的數據庫按我想要的方式更新。 但是在我的 web 主機上,當我嘗試運行時出現此錯誤
php artisan migrate
Migrating: 2022_09_15_092133_create_video_categorie_table
Migrated: 2022_09_15_092133_create_video_categorie_table (7.91ms)
Migrating: 2022_09_15_115815_add_foreign_key_to_media_table
Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1005 Can't create table `stag_db`.`media` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `media` add constraint `media_video_categorie_id_foreign` foreign key (`video_categorie_id`) references `video_categorie` (`id`))
at vendor/laravel/framework/src/Illuminate/Database/Connection.php:712
708▕ // If an exception occurs when attempting to run a query, we'll format the error
709▕ // message to include the bindings with SQL, which will make this exception a
710▕ // lot more helpful to the developer instead of just the database's errors.
711▕ catch (Exception $e) {
➜ 712▕ throw new QueryException(
713▕ $query, $this->prepareBindings($bindings), $e
714▕ );
715▕ }
716▕ }
+9 vendor frames
10 database/migrations/2022_09_15_115815_add_foreign_key_to_media_table.php:20
Illuminate\Support\Facades\Facade::__callStatic("table")
+22 vendor frames
33 artisan:37
Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
有誰知道這是從哪里來的?
我檢查了我的本地和我的主機 web 版本的 php 和 mysql,它們並不相同。
本地 php:PHP 7.4.3
托管 php:PHP 7.3.33
本地 mysql:mysql 版本 8.0.30
托管 mysql:mysql Ver 15.1 Distrib 10.3.36-MariaDB
這些差異有問題嗎?
謝謝!
編輯
上次遷移(php artisan migrate:status):
| Yes | 2022_08_18_135729_add_fichier_column_to_contact_table | 32 |
| Yes | 2022_08_29_120103_add_contact_motif_name_column_to_contact_table | 33 |
| Yes | 2022_09_15_092133_create_video_categorie_table | 33 |
| No | 2022_09_15_115815_add_foreign_key_to_media_table | |
| No | 2022_09_15_120150_add_orph_video_column_to_media_table | |
+------+------------------------------------------------------------------------------------------------------+-------+
編輯
幾個小時后,我終於找到了問題所在; 如果它可以幫助其他人;)
在我的虛擬主機上,使用的默認存儲引擎是 MyISAM,所以我的新表 video_categorie 是用這個引擎創建的。
但是這個引擎不允許在表之間建立關系。
我創建表 video_categorie 的第一次遷移有效,但是當我嘗試進行第二次遷移以建立外鍵時,由於 video_categorie 引擎,它不起作用。
我聯系我的虛擬主機以將我的服務器 mysql 配置更改為默認引擎 InnoDB。
在等待他們回答我時,我剛剛運行了我的第一次遷移,然后手動更改表引擎(ALTER TABLE video_categorie ENGINE = InnoDB)。
最后我運行了第二次遷移,它工作了!!!
該錯誤通知您media
上的主鍵和video_categorie
上的外鍵的列類型不匹配。
我的建議是更改您的遷移並使用列定義助手。
創建視頻類別表
Schema::create('video_categorie', function (Blueprint $table) {
$table->id('id'); // using the id column type
$table->string('nom_fr', 50);
$table->string('nom_en', 50)->nullable();
$table->unsignedSmallInteger('ordre')->nullable();
$table->timestamps();
});
AddForeignKeyToMediaTable
Schema::table('media', function (Blueprint $table) {
$table->foreignId('video_categorie_id')->nullable();
$table->unsignedSmallInteger('ordre_video')->nullable();
$table->foreign('video_categorie_id')->references('id')->on('video_categorie');
});
更新
為清楚起見,問題在於列類型integer
和unsignedInteger
不兼容,因此您需要更改外鍵列類型以與主鍵列類型兼容。 上面顯示了如何進行映射。
此修改需要在您的AddForeignKeyToMediaTable遷移中完成,替換$table->unsignedInteger('video_categorie_id')->nullable();
完全聲明。 如果它仍然存在於任何地方的遷移中,您將繼續看到錯誤。 您不能為此語句添加新的rollback
遷移,因為遷移失敗,它從未執行過。
幾個小時后,我終於找到了問題所在; 如果它可以幫助其他人;)
在我的虛擬主機上,使用的默認存儲引擎是 MyISAM,所以我的新表 video_categorie 是用這個引擎創建的。
但是這個引擎不允許在表之間建立關系。
我創建表 video_categorie 的第一次遷移有效,但是當我嘗試進行第二次遷移以建立外鍵時,由於 video_categorie 引擎,它不起作用。
我聯系我的虛擬主機以將我的服務器 mysql 配置更改為默認引擎 InnoDB。
在等待他們回答我時,我剛剛運行了我的第一次遷移,然后手動更改表引擎(ALTER TABLE video_categorie ENGINE = InnoDB)。
最后我運行了第二次遷移,它工作了!!!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.