programing

대규모 마이그레이션:두 번째 외래 키가 phpMyAdmin에서 보조 키로 생성되지 않음

javajsp 2023. 7. 28. 21:48

대규모 마이그레이션:두 번째 외래 키가 phpMyAdmin에서 보조 키로 생성되지 않음

설명:

피벗 테이블을 만들 때 Laravel 마이그레이션 문제가 발생했습니다.두 개의 외부 키와 복합 기본 키로 피벗 테이블을 정의하는 마이그레이션이 있습니다.하지만 제가 phpMyAdmin에서 마이그레이션을 실행하고 테이블을 확인했을 때 하나의 외부 키만 보조 키로 생성되고 다른 하나는 생성되지 않습니다.

코드:

<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('endpoint_parameter', function (Blueprint $table) {

            // Columns creation:
            $table->unsignedBigInteger('id_parameter');
            $table->unsignedBigInteger('id_endpoint');

            // Set Primary Key
            $table->primary(['id_parameter', 'id_endpoint']);

            // Add columns 'created_at' and 'updated_at'
            $table->timestamps();

            // Set foreign key referring to endpoint table
            $table->foreign('id_endpoint', 'fk_endpoint')
                ->references('id_endpoint')->on('endpoint')
                ->onUpdate('cascade')->onDelete('cascade');

                // Set foreign key referring to parameter table 
            $table->foreign('id_parameter', 'fk_parameter')
            ->references('id_parameter')->on('parameter')
            ->onUpdate('cascade')->onDelete('cascade');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('endpoint_parameter');
    }
};

예상 동작:

피벗 테이블 endpoint_parameter에 있는 외부 키(id_parameter와 id_endpoint)는 모두 phpMyAdmin에서 보조 키로 생성되어야 합니다.

인덱스 목록에서 BTREE 키: kf_parameter를 검색하려고 하는데 후자가 검색되지 않습니다.

실제 동작:

마이그레이션을 실행한 후 phpMyAdmin에서 테이블을 확인해보니 보조 키로 하나의 외부 키(id_endpoint)만 생성됩니다.두 번째 외부 키(id_parameter)는 보조 키로 생성되지 않습니다.

텍스트로 내보낸 테이블:

-- Table structure for table `endpoint_parameter`
CREATE TABLE `endpoint_parameter` (
  `id_parameter` bigint(20) UNSIGNED NOT NULL,
  `id_endpoint` bigint(20) UNSIGNED NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Indexes for dumped tables

-- Indexes for table `endpoint_parameter`
ALTER TABLE `endpoint_parameter`
  ADD PRIMARY KEY (`id_parameter`,`id_endpoint`),
  ADD KEY `fk_endpoint` (`id_endpoint`);

-- Constraints for dumped tables

-- Constraints for table `endpoint_parameter`
ALTER TABLE `endpoint_parameter`
  ADD CONSTRAINT `fk_endpoint` FOREIGN KEY (`id_endpoint`) REFERENCES `endpoint` (`id_endpoint`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `fk_parameter` FOREIGN KEY (`id_parameter`) REFERENCES `parameter` (`id_parameter`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;

추가 정보:

라라벨 버전: "라라벨/프레임워크": "^9.19" phpMyAdmin 버전: 5.2.1 데이터베이스:마리아DB

마이그레이션을 이미 여러 번 실행하고 캐시를 지우려고 시도했지만 문제가 계속 발생합니다.기본 키 선언 $table->primary([id_parameter', 'id_endpoint'])를 추가해도 복합 기본 키가 예상대로 생성되지 않는 것 같습니다.코드에서 기본 키를 제거하려고 시도했는데 두 개의 외래 키가 모두 올바르게 연결되어 있으므로 시스템에서 둘 다 올바르게 식별됩니다(구문 관련 코드에 오류 없음).

SQL 명령 "SHOW INDEX FROM endpoint_parameter" 결과:

  0  PRIMARY  1  id_parameter     A  0  NULL  NULL  BTREE
  0  PRIMARY  2  id_endpoint      A  0  NULL  NULL  BTREE
  1  fk_endpoint  1  id_endpoint  A  0  NULL  NULL  BTREE

언급URL : https://stackoverflow.com/questions/76626968/laravel-migration-second-foreign-key-not-created-as-secondary-key-in-phpmyadmin