Laravel 迁移创建自定义外键

Laravel migration create custom foreign key

我正在 laravel 中创建迁移,有时我需要在数据库中创建自定义外键名称。我搜索了这个解决方案,但没有与我的问题相似的问题。

我想为 names table 添加包含 birth_city_id、birth_state_id 和 birth_country_id 的列。除此之外我还想添加 death_city_id, death_state_id, death_counrty_id.

错误:

Migrating: 2021_11_21_130238_create_names_table

   Illuminate\Database\QueryException 

  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 (SQL: alter table `names` add constraint `birth_city_id` foreign key (`city_id`) references `cities` ())

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:703
    699▕         // If an exception occurs when attempting to run a query, we'll format the error
    700▕         // message to include the bindings with SQL, which will make this exception a
    701▕         // lot more helpful to the developer instead of just the database's errors.
    702▕         catch (Exception $e) {
  ➜ 703▕             throw new QueryException(
    704▕                 $query, $this->prepareBindings($bindings), $e
    705▕             );
    706▕         }
    707▕     }

      +9 vendor frames 
  10  database/migrations/2021_11_21_130238_create_names_table.php:37
      Illuminate\Support\Facades\Facade::__callStatic()

      +21 vendor frames 
  32  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

下面是相关文章,但它不起作用。 https://www.itsolutionstuff.com/post/laravel-migration-custom-foreign-key-name-exampleexample.html

Schema::create('names', function (Blueprint $table) {
            $table->id();
            $table->string('fname')->nullable();
            $table->string('mname')->nullable();
            $table->string('lname')->nullable();
            $table->string('image')->nullable();
            $table->string('nickname')->nullable();
            $table->string('height')->nullable();
            $table->string('gender',15)->nullable();
            $table->date('dob')->comment('Date of Birth')->nullable();
            $table->date('dod')->comment('Date of Death')->nullable();   
            $table->unsignedBigInteger('city_id');
            $table->unsignedBigInteger('state_id');
            $table->unsignedBigInteger('country_id');              
            $table->foreign('city_id','birth_city_id')->nullable()->refrences('id')->on('cities');
            $table->foreign('state_id','birth_state_id')->nullable()->refrences('id')->on('states');
            $table->foreign('country_id','birth_country_id')->nullable()->refrences('id')->on('countries');            
            $table->text('mini_bio')->nullable();
            $table->boolean('is_active')->nullable();
            $table->softDeletes();
            $table->timestamps();
        });

Table 城市、州、国家已存在于数据库中。

Schema::create('cities', function (Blueprint $table) {
        $table->id();
        $table->string('name')->nullable();
        $table->string('code',10)->nullable();
        $table->foreignId('state_id')->constrained();
        $table->softDeletes();
        $table->timestamps();
});

方法foreign将string|array作为$columns作为第一个参数,然后是一个字符串作为索引名称...

无论如何,如果您想对另一个 table 进行多重引用,您应该为此设置多列:

 $table->unsignedBigInteger('birth_city_id')->nullable();;
 $table->unsignedBigInteger('death_city_id')->nullable();;
 $table->foreign('birth_city_id')->references('id')->on('cities');
 $table->foreign('death_city_id')->references('id')->on('cities');

顺便说一句,我发现你的代码中有一个拼写错误,它 references 不是 refrences