添加外键时出现laravel/sql错误

2nc8po8w  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(362)

我试图在一些表之间建立一些关系,但有一个错误指出。。
illuminate\database\queryexception:sqlstate[42000]:语法错误或访问冲突:1064您的sql语法有错误;检查与mysql服务器版本相对应的手册,以获得在第1行(sql:alter table)的“)”附近使用的正确语法 users 添加约束 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');
}

这张table应该和这张table有关。。。

<?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');
}
}

我已经删除了表和模式,重新开始,仍然得到相同的错误!请善待我:!

wa7juj8i

wa7juj8i1#

不能使用定义列 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');

这同样适用于 boards 迁移。

hujrc8aj

hujrc8aj2#

我经历了非常相似的情况,我的模式`
公共函数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();
}

`我也一样

我通过授予特定用户对特定数据库的访问权限来解决这个问题,瞧,一切都开始像魅力一样运转起来

ktecyv1j

ktecyv1j3#

试试这个:

// 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');
    }
}

相关问题