Laravel 迁移创建自定义外键

Hem*_*mar 1 php migration laravel laravel-migrations laravel-8

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

\n

我想添加列names包含birth_city_id、birth_state_id 和birth_country_id 的表添加列。除此之外,我还想添加 Death_city_id、death_state_id、death_county_id。

\n

错误:

\n
Migrating: 2021_11_21_130238_create_names_table\n\n   Illuminate\\Database\\QueryException \n\n  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` ())\n\n  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:703\n    699\xe2\x96\x95         // If an exception occurs when attempting to run a query, we\'ll format the error\n    700\xe2\x96\x95         // message to include the bindings with SQL, which will make this exception a\n    701\xe2\x96\x95         // lot more helpful to the developer instead of just the database\'s errors.\n    702\xe2\x96\x95         catch (Exception $e) {\n  \xe2\x9e\x9c 703\xe2\x96\x95             throw new QueryException(\n    704\xe2\x96\x95                 $query, $this->prepareBindings($bindings), $e\n    705\xe2\x96\x95             );\n    706\xe2\x96\x95         }\n    707\xe2\x96\x95     }\n\n      +9 vendor frames \n  10  database/migrations/2021_11_21_130238_create_names_table.php:37\n      Illuminate\\Support\\Facades\\Facade::__callStatic()\n\n      +21 vendor frames \n  32  artisan:37\n      Illuminate\\Foundation\\Console\\Kernel::handle()\n
Run Code Online (Sandbox Code Playgroud)\n

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

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

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

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

OMR*_*OMR 7

方法foreign将string|array作为$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');
Run Code Online (Sandbox Code Playgroud)

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