I'm trying to link the thread
table to the message
table but when migrating, I get an error that says:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `thread` add constraint `thread_id_foreign` foreign key (`id`) references `message` (`thread_id`))
What am I doing wrong and how can I achieve this?
users migration:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email')->unique();
$table->string('full_name');
$table->string('password');
$table->string('bio');
$table->rememberToken();
$table->timestamps();
});
}
Here's the thread migration:
Schema::create('thread', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->foreign('id')
->references('thread_id')
->on('message');
});
Here's message migration:
Schema::create('message', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('user_id');
$table->unsignedInteger('thread_id');
$table->string('body');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});