-1

I'am trying to create Advertisement table which contain 'user_id' column referencing to 'id' column in users table, but with no luck.

as of the error message, i noticed that framework not passing column 'id' of the users table. Knowing i manged to solve this issue manually by using "phpMyAdmin".

I am wondering if there is any other way to solve this issue?

Users Schema:
       Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

Advertisements Schema:
        Schema::create('advertisements', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id');
        $table->string('name');
        $table->text('description');
        $table->mediumtext('image')->nullabe;
        $table->timestamps();

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

Error:

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").
PDO::prepare("alter table `advertisements` add constraint `advertisements_user_id_foreign` foreign key (`user_id`) references `users` () on delete cascade")
Salim
  • 62
  • 1
  • 8

1 Answers1

2

the problem is in your Advertisements migration code, you should put it ->references('id') instead of ->reference('id')

Advertisements Schema:
        Schema::create('advertisements', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id');
        $table->string('name');
        $table->text('description');
        $table->mediumtext('image')->nullabe;
        $table->timestamps();

        $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');
    });
Marwane Ezzaze
  • 1,032
  • 5
  • 11