【问题标题】:Laravel 4 Migration has syntax error when adding Foreign Key添加外键时,Laravel 4 Migration 出现语法错误
【发布时间】:2014-04-07 08:30:18
【问题描述】:

我正在尝试创建一个数据透视表来保存一些基本 ACL 功能的关系数据。

迁移类:

Schema::create('group_user', function($table)
{
    $table->increments('id');
    $table->unsignedInteger('group_id');
    $table->unsignedInteger('user_id');
    $table->timestamps();
    $table->softDeletes();
});

Schema::table('group_user', function($table)
{
    $table->foreign('group_id')
        ->reference('id')->on('groups');
    $table->foreign('user_id')
        ->reference('id')->on('users');
});

运行迁移命令后,出现以下错误:

  [Illuminate\Database\QueryException]
  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at li
  ne 1 (SQL: alter table `group_user` add constraint group_user_group_id_foreign foreign key (`group_id`) references `groups` ())

如您所见,添加外键约束的 SQL 语法缺少引用表的“id”列名。这是 Laravel 中的错误还是我的架构代码有问题?

【问题讨论】:

    标签: php mysql laravel-4


    【解决方案1】:

    所以我终于想通了,现在我觉得自己真的很愚蠢。这是我的拼写错误。

    应该是references() 而不是reference()

    【讨论】:

      【解决方案2】:

      这是您的代码中未引用的“group_user”表上的更改表的语法错误。

      【讨论】:

      • 抱歉,好像我粘贴了错误的迁移代码。我已经编辑了我的问题
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-19
      • 2019-02-09
      • 2013-11-16
      • 2014-10-10
      • 1970-01-01
      • 2017-05-06
      相关资源
      最近更新 更多