Error creating foreign key of a table

0

You see, I have a Targeta table in my project with the following code:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTargetasTable extends Migration{
    public function up(){
        Schema::create('targetas', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('codigo');
            $table->integer('saldo');
            $table->timestamps();
        });
    }

    public function down(){
        Schema::dropIfExists('targetas');
    }
}

And it is related to the User table:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration{
    public function up(){
        Schema::create('users', function (Blueprint $table){
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->unsignedInteger('cuenta');
            $table->foreign('cuenta')->references('id')->on('targetas');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    public function down(){
        Schema::dropIfExists('users');
    }
}

But I find this when doing the migration:

What will I be doing wrong?

Edit: I have added a photo with my creation files of the tables.

    
asked by Miguel Alparez 14.03.2018 в 18:11
source

1 answer

1

As I did not understand the explanation in the comments I added it as an answer, I am quite sure that the reason is the following:

The problem comes because you are trying to create a forenign key in relation to a table that does not exist, in this case targetas .

In the image that you attach, you see that all the tables were dropped, then the migration table was created and then the creation migration of the Users table was executed and that error is being thrown, before the migration that creates the table targetas to then use your id as foreign in the table User .

And this probably happened because you edited the migration of the table user that you created by default the command php artisan make:auth

Laravel runs the migrations based on the date of creation that has the same name.

If you are just starting the project, the easiest solution is to change the date of the targetas migration so that it is executed before that of the user table and to start the migrations again.

I hope it's useful, regards.

EDIT BASED ON COMMENT

@MiguelAlparez, I do not see that you have tried what I said in my comments, the migration of your table users is called 2014_10_15_000000_create_users_table.php and that of your table targetas 2018_03_14_etc_etc.php , at least in the photo that Attached you have not changed the dates to the names of the files. Therefore, the migration of the table users is still executed before that of your table targetas .

Name the migration of the targeting table and it should work:

2013_01_01_000000_create_targetas_table.php

and then run the following (Ojo, This erases all the tables in your base and returns all the migrations):

php artisan migrate:fresh

I hope it serves you.

    
answered by 14.03.2018 в 21:52