codehacker's avatar

column "foreign_key" doesn't exist in database

I am learning Laravel, YouTube "30 Days to Learn Laravel". In video number 12 at 05:51, if I click onto "Structure", there is no column named "foreign_key" in my SQLite database.

It is really difficult to find the bug, if I don't know, where to search.

This is the content of the file "2024_06_14_153747_create_tags_table.php" in the migration folder:

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('tags', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->timestamps();
        });

        Schema::create('job_tag', function (Blueprint $table) {
            $table->id();
            $table->foreignIdFor(\App\Models\Job::class, 'job_listing_id')->constrained()->cascadeOnDelete();
            $table->foreignIdFor(\App\Models\Tag::class)->constrained()->cascadeOnDelete();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('tags');
        Schema::dropIfExists('job_tag');
    }
};

After that I wrote "php artisan migrate:rollback && php artisan migrate" as shown in the YouTube video. But the column "foreign_key" doesn't show up.

And I can't continue learning. Where can I search?

0 likes
6 replies
jlrdw's avatar

Download the code from Github and do a compare.

1 like
Tray2's avatar

Have you added this to your Job model?

protected $table = 'job_listings';
1 like
Snapey's avatar
Snapey
Best Answer
Level 122

There shouldn't be a column called foreign_id, Thats something added by the program Jeff is using to display the tables (table plus I think)

1 like
codehacker's avatar

I did a compare. Unfortunately the following files are not included in github:

  • Model "Tag.php"
  • Factory "TagFactory.php"
  • migrations "2024_06_14_153747_create_tags_table.php"

Yes, in my Job model the following line is included: protected $table = 'job_listings';

foreign_id or foreign_key? In the video I see "foreign_key". Are you saying that everything is right? That it's just not being displayed? In the TablePlus program? I use the linux 20.04 version, Jeffrey uses the iOS version in the video.

Snapey's avatar

@codehacker

sorry yes, foreign_key , and it only appears when there is a linked record present in the table.

there is no column named "foreign_key" in my SQLite database.

Its not a column in the database table, its only a attribute of two of the rows and displayed by table plus

1 like
codehacker's avatar

It seems that the difference is entirely due to the GUI, Linux version vs iOS version, trusting Snapey's statement "There shouldn't be a column called foreign_id" I went ahead, in the video, and it all worked.

Many thanks for your answers.

Please or to participate in this conversation.