我正在與您聯系,因為在花了很多時間之后我絕對找不到解決方案......
這是我的問題:
我很自然地在 Laravel 5.3(恢復專案)上創建了我的表,以迎接新功能的到來。
設定完模型/控制器/CRUD 后,我注意到 ORM 存在問題。當我去創建帶有約束的表時,外鍵通過 orm. 它沒有完全應用它。也就是說,它將鍵定義為索引,但不會創建約束......我嘗試使用DB::statement但它給出了相同的結果。
我試圖更新學說/dblab,但沒有奏效。您認為 MySQL 版本可能是原因嗎?設定有問題嗎?知道之前的遷移存在約束!
在此先感謝那些幫助我的人!
看: Img:HeidiSQL 的觀點
應用型別:
Schema::create('application_types', function (Blueprint $table) {
//Ids
$table->increments('id');
// details
$table->char('type', 50)->unique();
// dates
$table->timestamp('created_at')->useCurrent(); // useCurrent equals to -> default(DB::raw('CURRENT_TIMESTAMP'))
$table->timestamp('updated_at')->nullable()->default(DB::raw('NULL ON UPDATE CURRENT_TIMESTAMP'));
});
應用類別:
Schema::create('application_categories', function (Blueprint $table) {
//Ids
$table->increments('id');
// details
$table->char('name', 50)->unique();
// dates
$table->timestamp('created_at')->useCurrent(); // useCurrent equals to -> default(DB::raw('CURRENT_TIMESTAMP'))
$table->timestamp('updated_at')->nullable()->default(DB::raw('NULL ON UPDATE CURRENT_TIMESTAMP'));
});
應用:
Schema::create('applications', function (Blueprint $table) {
//Ids
$table->increments('id', true);
$table->integer('type_id')->unsigned(); // -> link to application types table
$table->integer('document_id')->unsigned(); // -> link to documents table
$table->integer('category_id')->unsigned(); // -> link to application category table
$table->integer('referent_id')->unsigned(); // -> link to dir_people table
$table->integer('created_by_user_id')->unsigned(); // -> link to users table
$table->integer('updated_by_user_id')->unsigned()->nullable(); // -> link to users table
// details
$table->char('name', 50);
$table->longtext('description');
$table->longtext('path');
$table->boolean('is_active')->default(1);
// dates
$table->timestamp('created_at')->useCurrent(); // useCurrent equals to -> default(DB::raw('CURRENT_TIMESTAMP'))
$table->timestamp('updated_at')->nullable()->default(DB::raw('null ON UPDATE CURRENT_TIMESTAMP'));
});
// == Set foreign keys ==
Schema::table('applications', function (Blueprint $table) {
//Document
$table->unique('document_id');
$table->foreign('document_id')
->references('id')->on('documents')
->onDelete('cascade');
// Application type
$table->foreign('type_id')
->references('id')->on('application_types');
// Application Category
$table->foreign('category_id')
->references('id')->on('application_categories');
// Referent_id
$table->foreign('referent_id')
->references('id')->on('dir_people');
// Created by User
$table->foreign('created_by_user_id')
->references('id')->on('users');
// Updated by User
$table->foreign('updated_by_user_id')
->references('id')->on('users');
});
uj5u.com熱心網友回復:
【帖子作者】
我檢查了是否可以直接在 MySql 上創建它,我驚訝地發現它不起作用。
所以ORM端的代碼是好的!
但是我找到了解決方案,它可以幫助某人。
通過切換到InnoDB作為引擎,它可以作業
如果您使用 WAMP,請檢查安裝是否完成!(我的是)
然后添加到app/config/database:
'connections' => [
'mysql' => [
//...
'engine' => 'InnoDB'
]
]
別的
在您的遷移檔案中:
$table->engine = 'InnoDB';
絕對有必要使用InnoDB創建所有表,否則它不起作用!
uj5u.com熱心網友回復:
外鍵不是在資料庫中創建的,因為它必須參考一個主鍵,女巫我不認為你的遷移是這種情況
嘗試在您的表中添加:
$table->primary('id');
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/374272.html
