1

Laravel 5.6 running on: 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

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; 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)

Errors thrown by 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

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

8
  • Other tables / migrations to run properly right? So nothing wrong with the setup? Commented Aug 24, 2018 at 19:50
  • try: $table->Integer('role_id')->unsigned() Commented Aug 24, 2018 at 19:50
  • Seems like Laravel is ignoring the ->references('id') for some reason. Without cascade, does it work? Commented Aug 24, 2018 at 19:51
  • @utdev Yes, all the other migrations running without any errors. @AlvaroAlves, I was already tried that and it gives the error like " PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table order-project.#sql-3228_8a (errno: 150 "Foreign key constraint is incorrectly formed")")" @Phiter Nope, it's not working either. By the way, Php Storm says "Method 'references' not found in Illuminate\Support\Fluent" but i think it is about the IDE not about the laravel be cause when i was work with VSC there is no error like that. Commented Aug 24, 2018 at 19:54
  • you could just test following stackoverflow.com/questions/31263637/… this will return your migration query as a raw sql query. Just to be sure that the query is correct Commented Aug 24, 2018 at 19:56

3 Answers 3

0

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');
Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't work for me, i got an error like: General error: 1005 Can't create table order-project.#sql-410_a (errno: 150 "Foreign key constraint is incorrectly formed")")
0

I think you have similiar problem with this post Laravel foreign key onDelete('cascade') not working

Hope this can help

2 Comments

Thanks, but it doesn't work either. Error is: errno: 150 "Foreign key constraint is incorrectly formed")
Did you change the engine to InnoDB as suggested in the question?
0

I solved the problem. It fails because of Laravel trying to migrate user_roles table before the roles table. I just migrate roles table before the user_roles table and it's worked! Before the solution rules table is not throwing an error but it's not completely created.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.