0

I am using Laravel 6. I created a few migrations but I can't let them run successfully. These are my migrations.

    public function up()
{
    Schema::create('nationalities', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->timestamps();
        $table->engine = "InnoDB";
    });


    public function up()
{
    Schema::create('genders', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->timestamps();
        $table->engine = "InnoDB";
    });


    public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('firstname');
        $table->string('lastname');
        $table->string('street');
        $table->string('zip');
        $table->string('city');
        $table->date('birthdate');
        $table->string('gender');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
        $table->unsignedBigInteger('gender_id');
        $table->unsignedBigInteger('nationality_id');
        $table->engine = "InnoDB";
        $table->foreign('gender_id')->references('id')->on('gender');
        $table->foreign('nationality_id')->references('id')->on('nationality');
    });
}

After executing php artisan migrate I get this error message:

General error: 1215 Cannot add foreign key constraint (SQL: alter table users add constraint users_gender_id_foreign foreign key (gender_id) references gender (id))

What am I doing wrong?

Umut Savas
  • 113
  • 1
  • 14
  • you can [refer](https://stackoverflow.com/questions/16969060/mysql-error-1215-cannot-add-foreign-key-constraint) – Barbaros Özhan Nov 13 '19 at 21:40
  • this can't be the problem. I am using unsignedBigInteger for bigIncrements. what should I use instead? – Umut Savas Nov 13 '19 at 21:43
  • have you read the secondly ranked answer also ? – Barbaros Özhan Nov 13 '19 at 21:46
  • yes. 1. I am using innodb for every table. 2. I am referencing an existing key 3. The types of the columns are the same 4. The PK or FK is not a varchar. what are you trying to tell me? – Umut Savas Nov 13 '19 at 21:49
  • 1
    try running each migration individually. sometimes when i try to run migrations like that, it'll try to run the migration that has the foreign key before it runs the tables that it's referencing. So when you run them individually, do the ones that don't have foreign keys first then run the foreign key last, see if that helps – maximus1127 Nov 13 '19 at 21:50
  • you may have no matching value in the parent table then. A query containing `not exists` or `not in` can be used. – Barbaros Özhan Nov 13 '19 at 21:52

1 Answers1

1

Your table name is genders not gender and nationalities not nationality:

Change:

...->references('id')->on('gender');
...->references('id')->on('nationality');

To:

...->references('id')->on('genders');
...->references('id')->on('nationalities');
lagbox
  • 48,571
  • 8
  • 72
  • 83