简体   繁体   English

Laravel迁移外键错误

[英]Laravel Migration Foreignkey Error

Laravel 5.6 running on: PHP 7.2, MariaDB 10.3 Laravel 5.6运行于:PHP 7.2,MariaDB 10.3

When I want to set foreign key for my table, I just keep taking that error. 当我想为我的表设置外键时,我只会继续犯该错误。

On other tables, all id variables defined by Laravel itself and auto increments unsigned 在其他表上,Laravel本身定义的所有id变量和自动递增的无符号

So, my Migration is like this: 所以,我的迁移是这样的:

public function up()
{

    Schema::create('user_roles', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('role_id');
        $table->unsignedInteger('user_id');

        $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

The error is like this : 错误是这样的

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法有错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') on delete cascade' at lin e 1 (SQL: alter table user_roles add constraint user_roles_role_id_foreign foreign key ( role_id ) references roles () on delete cascade) 检查与您的MariaDB服务器版本相对应的手册以在lin e 1处在删除级联上使用')附近的正确语法(SQL:alter table user_roles add约束user_roles_role_id_foreign外键( role_id )在删除级联上引用roles ())

Errors thrown by Laravel : Laravel抛出的错误

1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ') on delete cascade' at line 1") C:\\xampp\\htdocs\\order-project\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:452 1 PDOException::(“ SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;请查看与MariaDB服务器版本相对应的手册,以了解在删除级联上')附近使用的正确语法'在第1行“)C:\\ xampp \\ htdocs \\ order-project \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Database \\ Connection.php:452

2 PDO::prepare("alter table user_roles add constraint user_roles_role_id_foreign foreign key ( role_id ) references roles () on delete cascade") C:\\xampp\\htdocs\\order-project\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:452 2 PDO :: prepare(“更改表user_roles添加约束user_roles_role_id_foreign外键( role_id )引用删除级联上的roles ()“)C:\\ xampp \\ htdocs \\ order-project \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Database \\ Connection.php:452

try this 尝试这个

$table->integer('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');

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

I think you have similiar problem with this post Laravel foreign key onDelete('cascade') not working 我认为您对这篇文章Laravel外键onDelete('cascade')不起作用有类似问题

Hope this can help 希望这可以帮助

I solved the problem. 我解决了问题。 It fails because of Laravel trying to migrate user_roles table before the roles table. 由于Laravel尝试在角色表之前迁移user_roles表,因此失败。 I just migrate roles table before the user_roles table and it's worked! 我只是将角色表迁移到user_roles表之前,并且它起作用了! Before the solution rules table is not throwing an error but it's not completely created. 在解决方案规则表未引发错误之前,但未完全创建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM