【发布时间】:2018-11-25 23:14:14
【问题描述】:
tl;博士。解决方案:
感谢Jonas。
问题是我提到的外国表不是 InnoDB。
我在 alter migrations 中添加了原始 SQL 语句,然后添加了外键:
DB::statement("ALTER TABLE table ENGINE='InnoDB';");
原始问题
首先,在 Stackoverflow 警察逮捕我之前,我知道这个问题可能是这个网站数据库的 83%。但我很特别(开玩笑,我知道我不是)。但是我已经尝试了大多数常见的东西,但似乎没有任何效果。所以可能我正在监督一些事情。
错误
一般错误:1215 无法添加外键约束(SQL:alter table applications 添加约束 applications_user_id_foreign 外键 (user_id) 在删除级联时引用 users (id))
这是我的迁移:
public function up()
{
Schema::create("applications", function(Blueprint $table) {
$table->engine = "InnoDB";
$table->increments('id');
$table->timestamps();
});
Schema::table('applications', function($table) {
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('job_request_id')->unsigned()->index();
$table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
$table->integer('status')->default(0);
});
}
我已经尝试过的:
1.
public function up()
{
Schema::create("applications", function(Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('job_request_id')->unsigned();
$table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
$table->integer('status')->default(0);
});
}
2.
public function up()
{
Schema::create("applications", function(Blueprint $table) {
$table->engine = "InnoDB";
$table->increments('id');
$table->timestamps();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('job_request_id')->unsigned();
$table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
$table->integer('status')->default(0);
});
}
- 将迁移拆分为两个文件(一个 create 和一个 alter)。甚至一个一个地添加每个引用。
4.- 使用 DB::statement('SET FOREIGN_KEY_CHECKS=0;');和 =1 在迁移的开头和结尾。
5.- 删除 unsigned() 和 index()。
可能意味着什么:
1.- 当我回滚迁移时,它不会删除表。所以如果我回滚和迁移,会给我一个“已经存在的错误”。
2.- 我已经有引用相同项目的迁移,即:
Schema::create('job_requests', function (Blueprint $table) {
...
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');
...
});
更新
对于我尝试过的放置方法:
-
对于创建迁移
公共函数down() { Schema::drop('应用程序'); }
公共函数down() { Schema::dropIfExists('applications'); }
2.- 对于变更迁移
public function down()
{
Schema::table('applications', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropColumn('user_id');
$table->dropForeign(['job_request_id']);
$table->dropColumn('job_request_id');
});
}
更新 2:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
public function up()
{
Schema::create('job_requests', function (Blueprint $table) {
$table->increments('id');
$table->integer('status')->default(0);
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('job_requests');
}
我又添加了三个变更迁移:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->engine = "InnoDB";
});
}
///////////////////////////
public function up()
{
Schema::table('job_requests', function (Blueprint $table) {
$table->engine = "InnoDB";
});
}
///////////////////////////
public function up()
{
Schema::table('applications', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('applications', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['job_request_id']);
});
}
还没有运气。
【问题讨论】:
-
您能否为您的迁移展示
down方法?另外,当你说它不会删除表时,我假设你想要它? -
(我已经更新了问题)。是的,我的意思是,回滚时将其删除。
-
您的问题不包含实际问题。哪个外键不起作用?什么错误?
-
两者。 (我也分别尝试过)
-
你不能这样改变表引擎。您必须使用原始 SQL:
DB::statement("ALTER TABLE users ENGINE='InnoDB';");
标签: mysql laravel database-migration