【发布时间】: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