S
S
smilelan2020-02-07 21:21:58
Laravel
smilelan, 2020-02-07 21:21:58

How to connect the bases correctly?

I'm trying to link 2 tables, during migration I get an error SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "roles" does not exist (SQL: alter table "users" add constraint "users_role_id_foreign" foreign key ("role_id") references "roles "("id_role"))
. I don't understand what's the matter

class CreateUsersTable extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('role_id')->unsigned();
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();

            $table->foreign('role_id')->references('id_role')->on('roles');
        });
    }
    public function down()
    {
        Schema::dropIfExists('users');
    }
}

class CreateRolesTable extends Migration
{
    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->integer('id_role');
            $table->timestamps();
        });
    }

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

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question