Laravel 字符串外键格式不正确
Laravel strings Foreign Key incorrectly formed
我正在尝试在两个表之间创建一个 fk。这是我的迁移:
public function up()
{
Schema::create('contracts', function (Blueprint $table) {
$table->increments('id');
// Some other cols removed for this post
$table->string('user_file_path');
$table->timestamps();
$table->foreign('user_file_path')->references('path')->on('user_files');
});
}
和
public function up()
{
Schema::create('user_files', function (Blueprint $table) {
$table->increments('id');
$table->integer('application_id')->unsigned();
$table->string('random', 32);
$table->string('path');
$table->timestamps();
$table->foreign('application_id')->references('id')->on('applications');
});
}
我在 运行 php artisan migrate
之后收到此错误
General error: 1005 Can't create table 'dashboard'.'#sql-1890_b6' (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table 'contracts' add constraint 'contracts_user_file_path_foreign' foreign key ('user_file_path') references 'user_files' ('path'))
我做错了什么?
为什么不通过 id
引用 user_files
记录,例如
public function up()
{
Schema::create('contracts', function (Blueprint $table) {
$table->increments('id');
// Some other cols removed for this post
$table->integer('user_file_id')->unsigned();
$table->timestamps();
$table->foreign('user_file_id')->references('id')->on('user_files');
});
}
我正在尝试在两个表之间创建一个 fk。这是我的迁移:
public function up()
{
Schema::create('contracts', function (Blueprint $table) {
$table->increments('id');
// Some other cols removed for this post
$table->string('user_file_path');
$table->timestamps();
$table->foreign('user_file_path')->references('path')->on('user_files');
});
}
和
public function up()
{
Schema::create('user_files', function (Blueprint $table) {
$table->increments('id');
$table->integer('application_id')->unsigned();
$table->string('random', 32);
$table->string('path');
$table->timestamps();
$table->foreign('application_id')->references('id')->on('applications');
});
}
我在 运行 php artisan migrate
General error: 1005 Can't create table 'dashboard'.'#sql-1890_b6' (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table 'contracts' add constraint 'contracts_user_file_path_foreign' foreign key ('user_file_path') references 'user_files' ('path'))
我做错了什么?
为什么不通过 id
引用 user_files
记录,例如
public function up()
{
Schema::create('contracts', function (Blueprint $table) {
$table->increments('id');
// Some other cols removed for this post
$table->integer('user_file_id')->unsigned();
$table->timestamps();
$table->foreign('user_file_id')->references('id')->on('user_files');
});
}