Laravel字符串外键格式错误

时间:2018-10-18 17:03:52

标签: laravel laravel-5 laravel-migrations

我正在尝试在两个表之间创建一个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'))

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

您为什么不引用user_files的{​​{1}}记录,例如

id