添加外键时的Laravel / SQL错误

时间:2018-07-20 01:00:34

标签: mysql laravel reference constraints

我正在尝试在某些表之间建立一些关系,但是有一个错误指出。.

  

Illuminate \ Database \ QueryException:SQLSTATE [42000]:语法错误或   访问冲突:1064您的SQL语法错误;校验   与您的MySQL服务器版本相对应的手册   在第1行的')'附近使用正确的语法(SQL:alter table users add   约束users_board_id_foreign外键(board_id)   引用``())

class CreateUsersTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');

        $table->foreign('board_id')->unsigned();
        $table->foreign('board_id')->references('id')->on('boards');

        $table->foreign('message_id')->references('id')->on('messages');
        $table->foreign('message_id')->unsigned();

        $table->rememberToken();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('users');
}

该表应该与此表相关...

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBoardsTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('boards', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 100)->unique();

        $table->foreign('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');

        $table->foreign('link_id')->unsigned();
        $table->foreign('link_id')->references('id')->on('links');

        $table->foreign('message_id')->unsigned();
        $table->foreign('message_id')->references('id')->on('messages');

        $table->foreign('tag_id')->unsigned();
        $table->foreign('tag_id')->references('id')->on('tags');

        $table->timestamps();

    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('boards');
}
}

我已经删除了表和模式并重新开始,但仍然遇到相同的错误!请客气:!

3 个答案:

答案 0 :(得分:1)

您不能使用(1,2)定义列:

foreign()

$table->integer('board_id')->unsigned(); $table->foreign('board_id')->references('id')->on('boards'); $table->integer('message_id')->unsigned(); $table->foreign('message_id')->references('id')->on('messages'); 迁移也是如此。

答案 1 :(得分:0)

尝试一下:

// USERS

class CreateUsersTable extends Migration
    {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');

            $table->integer('board_id')->unsigned();
            $table->foreign('board_id')->references('id')->on('boards');

            $table->integer('message_id')->unsigned();
            $table->foreign('message_id')->references('id')->on('messages');


            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('users');
    }


    //BOARDS


    use Illuminate\Support\Facades\Schema;
    use Illuminate\Database\Schema\Blueprint;
    use Illuminate\Database\Migrations\Migration;

    class CreateBoardsTable extends Migration
    {
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('boards', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name', 100)->unique();

            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users');

            $table->integer('link_id')->unsigned();
            $table->foreign('link_id')->references('id')->on('links');

            $table->integer('message_id')->unsigned();
            $table->foreign('message_id')->references('id')->on('messages');

            $table->integer('tag_id')->unsigned();
            $table->foreign('tag_id')->references('id')->on('tags');

            $table->timestamps();

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('boards');
    }
}

答案 2 :(得分:0)

我经历了非常相似的情况,我的模式 `
公共功能up(){

    Schema::dropIfExists('countries_states');
    Schema::create('countries_states', function (Blueprint $table) {
        $table->id();
        $table->foreignId('country_id')->unsigned();
        $table->foreign('country_id')->references('id')->on('countries');
        $table->string('state', 30);
        $table->string('abbr', 30);
    });
    //Schema::enableForeignKeyConstraints();
}

` 而且我有相同的error screenshot

我通过授予特定用户对特定数据库和权限的访问权限来解决它-一切开始像魅力一样起作用 solution