Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Antonella's avatar

SQLSTATE[HY000]: General error: 3780 Referencing column 'lang_id' and referenced column 'id'

I created a many to many relationship between User and Language:

Models/User

public function languages()
{
    return $this->belongsToMany(Language::class);
}

Models/Language

 public function users()
{
    return $this->belongsToMany(User::class);
}

Migrations/Languages

Schema::create('languages', function (Blueprint $table) {
        $table->id();
        $table->text('name');
        $table->text('sigla');
        $table->timestamps();
    });

Migrations/Users

Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->text('img')->nullable();
        $table->rememberToken();
        $table->string('api_token', 64)->unique()->default(Str::random(50));
        $table->timestamps();
    });

Migrations/User_Language

Schema::create('user_language', function (Blueprint $table) {
        $table->id();
        $table->unsignedInteger('lang_id');
        $table->unsignedInteger('user_id');
        $table->timestamps();

        $table->unique('lang_id','user_id');

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


    });

unfortunately the latter gives me the following error:

 SQLSTATE[HY000]: General error: 3780 Referencing column 'lang_id' and referenced column 'id' in foreign key constraint 'user_language_lang_id_foreign' are incompatible. (SQL: alter table `user_language` add constraint `user_language_lang_id_foreign` foreign key (`lang_id`) references `languages` (`id`) on delete cascade)
0 likes
10 replies
Nakov's avatar

@gianmarx change them to this:

$table->unsignedBigInteger('lang_id');
$table->unsignedBigInteger('user_id');

since the default for ->id() is a big integer in the newer Laravel versions.

Make sure you add the table name in your relationships, since that won't work. Your table should be named language_user to work out of the box.

2 likes
Nakov's avatar
Nakov
Best Answer
Level 73

@gianmarx just name your pivot table language_user instead of user_language and then in the same table, instead of

$table->unsignedInteger('lang_id');
$table->unsignedInteger('user_id');

use this:

$table->unsignedBigInteger('lang_id');
$table->unsignedBigInteger('user_id');
1 like
Antonella's avatar

App\Models\User {#4281 id: 2, name: "Tommy", email: "[email protected]", email_verified_at: "2020-12-22 15:51:50", img: "logo.png", created_at: "2020-12-22 15:51:50", updated_at: "2020-12-22 15:51:50", }

>>> $a->language
=> null
>>> 

I created 5 elements related to this user with id 2 unfortunately when I go with tinker it gives me this problem it tells me null

@nakov

Nakov's avatar

@gianmarx that's why I told you that the table name should be language_user and not user_language..

so then just add the table name in here:

public function languages()
{
    return $this->belongsToMany(Language::class, 'user_language');
}
Antonella's avatar

I changed the name as you said unfortunately I always have the same error:

 Schema::create('language_user', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('lang_id');
        $table->unsignedBigInteger('user_id');

        $table->timestamps();

        $table->unique('lang_id','user_id');

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

but :

=> App\Models\User {#4281 id: 2, name: "Manfredi", email: "[email protected]", email_verified_at: "2020-12-22 15:51:50", img: "logo.png" created_at: "2020-12-22 15:51:50", updated_at: "2020-12-22 15:51:50", }

>>> $a->language
=> null
>>>

@nakov

Nakov's avatar

And it will always be null since you don't have any field language in your User model.

It should be $user->languages instead, look at the name of the function that you added in the User model.

And I helped you with the main issue, sorry but I don't have time to teach you basic in programming. There are more than 1800 videos here on Laracasts, start watching and learning.

Just an additional tip: make sure you close and start again Tinker session whenever you make a change in your model.

1 like
Antonella's avatar

i change this :

Schema::create('language_user', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('lang_id');
        $table->unsignedBigInteger('user_id');

        $table->timestamps();
        $table->foreign('lang_id')->on('languages')->onDelete('cascade');
        $table->foreign('user_id')->on('users')->onDelete('cascade');
    });



  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 ') on delete cascade' at line 1 (SQL: alter table `language_user` add constraint `language_user_lang_id_foreign` foreign key (`lang_id`) references `languages` () on delete cascade)

@nakov

Antonella's avatar

it'run

Schema::create('user_language', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('lang_id');
        $table->unsignedBigInteger('user_id');

        $table->timestamps();

        $table->unique('lang_id','user_id');

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

Please or to participate in this conversation.