ramsom
1 week ago
118
4
Laravel

UUID Foreign Key - Unable to add constraint

Posted 1 week ago by ramsom

I am trying to create a foreign key between space and user however I keep getting the following error:

SQLSTATE[HY000]: General error: 1824 Failed to open the referenced table 'spaces' (SQL: alter table users add constraint users_space_id_foreign foreign key (space_id) references spaces (id))

this article does reference a similar problem https://laracasts.com/discuss/channels/laravel/using-uuid-my-foreign-key-constraints-fail. However, it does not solve my problem. I am still a bit of a Noob so any help will be appreciated.

The users

Schema::create('users', function (Blueprint $table) {
        $table->uuid('id')->primary();
        $table->uuid('space_id')->nullable();
        $table->foreign('space_id')->references('id')->on('spaces');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();

    });

Space

Schema::create('spaces', function (Blueprint $table) {
    $table->uuid('id')->primary();
        $table->string('name');
        $table->timestamps();
    });

Please sign in or create an account to participate in this conversation.