【问题标题】:Laravel migration create custom foreign keyLaravel 迁移创建自定义外键
【发布时间】:2021-12-31 20:56:33
【问题描述】:

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

我想为包含birth_city_id、birth_state_id 和birth_country_id 的names 表添加列。除此之外我还要加上death_city_id、death_state_id、death_county_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();
        });

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

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();
});

【问题讨论】:

  • 我认为您错误地对迁移进行了编码。应该先写列,再单独写外关系。
  • @nagidi 我也试过了,但它抛出了一个错误。
  • 将外部放在底部,而不是 foreign()->references 等,您可以在 laravel 8 中执行此操作: $table->foreignId('your_id')->constrained('your表')->可空(); Nullable 也应该在后面。
  • 尝试从foreign 约束中删除nullable 并将其添加到unsignedBigInteger
  • @bgeertsema2000 如果您阅读文档 nullable() 必须在 constrained() 之前出现,如下所示:$table->foreignId('user_id')->nullable()->constrained();

标签: php laravel migration laravel-8 laravel-migrations


【解决方案1】:

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

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

 $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

【讨论】:

  • 更简单的方法是把它写成 foreignId('some_id')->constrained('table')->nullable();就像文档说的那样。
  • 我认为birth_city_id 是索引名称。
  • @OMR 当我们执行这行代码时,它将提交创建外键约束,因为我们没有提到 city_id 作为外键。我的意思是我想在这张表中存储出生城市、州、国家 ID 以及死亡城市、州和国家 ID。这就是我希望列名不同的原因。
  • mmm ,我已经更新了我的答案以适应,请告诉我它是否有用
  • 那个错字很容易漏掉
猜你喜欢
  • 2023-01-04
  • 1970-01-01
  • 2018-06-19
  • 1970-01-01
  • 2021-01-03
  • 2020-06-05
  • 2016-12-07
  • 2018-05-16
相关资源
最近更新 更多