siraj762
1 month ago
274
2
Laravel

FK

Posted 1 month ago by siraj762

how to use FK as string Schema::create('users', function (Blueprint $table) { // $table->bigIncrements('id'); $table->string('name'); $table->string("chat_id")->uniqid(); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); });

Schema::create('posts', function (Blueprint $table) {

        $table->bigIncrements('id');
        $table->string('chat_id');
        $table-> foreign('chat_id')->references("chat_id")->on("users")->onDelete('cascade');

        $table->text('body');
        $table->timestamps();
    });

my error

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table jway.posts (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table posts add constraint posts_chat_id_foreign foreign key (chat_id) references users (chat_id) on delete cascade)

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