0

我正在尝试在 Laravel 4、User 和 GP 上的两个表之间创建外键约束。一个用户可以与多个 GP 相关联。外键是 users 表中的“Practice_ID”,它与 GP 表中的 ID 相关联。

    public function up()
{

    // Creates GP table
    Schema::create('gp', function($ta)
    {
        $ta->bigIncrements('id');
        $ta->string('address_1');
        $ta->string('address_2');
        $ta->string('address_3');
        $ta->string('post_code');
        $ta->timestamps();
    });

    // Creates the users table
    Schema::create('users', function($table)
    {
        $table->increments('id');
        $table->string('username');
        $table->string('email');
        $table->string('password');
        $table->string('confirmation_code');
        $table->boolean('confirmed')->default(false);
        $table->bigInteger('practice_id');
        $table->foreign('practice_id')->references('id')->on('gp');
        $table->timestamps();
    });

    // Creates password reminders table
    Schema::create('password_reminders', function($t)
    {
        $t->string('email');
        $t->string('token');
        $t->timestamp('created_at');
    });

}

我得到的错误是:

[异常]
SQLSTATE [HY000]:一般错误:1005 无法创建表'doctor.#sql-3ac_a4'(错误号:150)(SQL:alter table usersadd constraint users_practice_id_foreign外键(practice_id)引用gpid))(绑定:数组(
))

4

1 回答 1

0

err 150很可能是数据类型不匹配。

将表中的外键删除为无符号整数users

$table->integer('practice_id')->unsigned();

还要验证您的数据库引擎是支持 FK 约束的 INNODB

于 2013-11-07T22:10:05.393 回答