#SOLVED Migration - Referenced column in foreign key incompatible

1 messages · Page 1 of 1 (latest)

jagged helm
#
        public function up(): void 
{
        Schema::create('media', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->integer('runtime')->nullable();
            $table->text('overview')->nullable();
            $table->timestamps();
        });

        Schema::create('lines', function (Blueprint $table) {
            $table->foreignId('media_id')->constrained()->cascadeOnDelete();
            $table->integer('number');
            $table->text('text');
            $table->integer('start');
            $table->integer('end');
            $table->primary(['media_id', 'number']);
        });

        Schema::create('occurences', function (Blueprint $table) {
            $table->id();
            $table->foreignId('media_id')->constrained()->cascadeOnDelete();
            $table->foreignId('number')->constrained('lines', 'number');
            $table->text('base_word');
            $table->text('literal');
            $table->timestamps();
        });
    }

Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 3780 Referencing column 'number' and referenced column 'number' in foreign key constraint 'occurences_number_foreign' are incompatible. (Connection: mysql, SQL: alter table occurences add constraint occurences_number_foreign foreign key (number) references lines (number))

The issue is certainly with this line $table->foreignId('number')->constrained('lines', 'number'); but I am really lost for ideas on how to fix it. Any ideas?

hybrid oracle
#

Check your column type. One may be a smallint instead of a bigint

jagged helm
#

You're right, they are mismatched.

  • occurences.number bigint unsigned NOT NULL
  • lines.number int NOT NULL,

I am unsure how to make them the same type while designating FK...

Would you be able to point me in the right direction?

hybrid oracle
jagged helm
#

I was able to fix it with these changes.

Firstly, lines.number requires an index.

$table->integer('number')->index();

Now, in the occurences table we can create the FK field in the ordinary declaraction. Attempts to do this in one move seem to default the type incorrectly. We have to be explicit and do it in two steps like this:

       Schema::create('occurences', function (Blueprint $table) {
            $table->id();
            $table->foreignId('media_id')->constrained()->cascadeOnDelete();
            //$table->foreignId('number')->constrained('lines', 'number'); don't do this, it won't work
            $table->integer('line_number');
            $table->text('base_word');
            $table->text('literal');
            $table->integer('start');
            $table->integer('end');
            $table->timestamps();
        });

        Schema::table('occurences', function (Blueprint $table) {
            $table->foreign('line_number', 'fk_occurences_lines_number')->references('number')->on('lines');
        });