Laravel 5.8迁移外键问题?

时间:2019-03-18 11:26:04

标签: foreign-keys database-migration laravel-5.8

我有2张桌子。我的代码在laravel 5.7上运行良好,但是当我使用laravel时。我总是会收到这样的错误。有人可以帮助我吗?

Schema::create('tb_satuan', function (Blueprint $table) {
        $table->bigIncrements('id_satuan');
        $table->string('nama_satuan',40);
        $table->timestamps();
    });

    Schema::create('tb_user', function (Blueprint $table) {
        $table->bigIncrements('id_user');
        $table->BigInteger('id_satuan')->unsigned();
        $table->string('username',20);
        $table->string('email',30);
        $table->text('password');
        $table->timestamps();

        $table->foreign('id_satuan')->reference('id_satuan')->on('tb_satuan');
    });

这是错误:

  

Illuminate \ Database \ QueryException:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有一个错误;请参见语法。   检查与您的MariaDB服务器版本相对应的手册,以找到在第1行('SQL':alter table tb_user添加约束tb_user_id_satuan_foreign外键(id_satuan)引用附近在')'处使用的正确语法tb_satuan())

2 个答案:

答案 0 :(得分:1)

是参考 S 不是参考

$table->foreign('id_satuan')->references('id_satuan')->on('tb_satuan');

答案 1 :(得分:0)

 public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->string('title');
            $table->string('slug')->unique();
            $table->string('image')->default('default.png');
            $table->text('body');
            $table->integer('amount');
            $table->integer('room');
            $table->boolean('status')->default(false);
            $table->foreign('user_id')
                ->references('id')->on('users')
                ->onDelete('cascade');
            $table->timestamps();
        });
    }