Ich habe diese beiden Datenbanktabellen:
Benutzertabelle wird solche Informationen verarbeiten
Schema::create('users', function (Blueprint $table) { $table->increments('id')->unique(); $table->string('email')->unique(); $table->string('username')->unique(); $table->string('password', 60); $table->string('photo')->nullable(); $table->integer('partner_id')->unsigned(); $table->foreign('partner_id')->references('id')->on('partners'); $table->rememberToken(); $table->timestamps(); });
Und die Partnertabelle enth?lt alle Benutzer-Metainformationen wie Vor- und Nachname usw.
Schema::create('partners', function (Blueprint $table) { /** * Identity Columns */ $table->increments('id')->unique(); $table->string('first_name'); $table->string('middle_name')->nullable(); $table->string('last_name')->nullable(); $table->string('display_name')->nullable(); $table->string('email')->unique()->nullable(); $table->string('website')->nullable(); $table->string('phone')->nullable(); $table->string('mobile')->nullable(); $table->string('fax')->nullable(); $table->date('birthdate')->nullable(); $table->longText('bio')->nullable(); $table->string('lang')->nullable(); //Language /** * Address Columns */ $table->text('street')->nullable(); $table->text('street2')->nullable(); $table->integer('country_id')->unsigned(); // foreign $table->foreign('country_id')->references('id')->on('countries'); $table->integer('state_id')->unsigned(); // foreign $table->foreign('state_id')->references('id')->on('country_states'); $table->string('city')->nullable(); $table->string('district')->nullable(); $table->string('area')->nullable(); $table->string('zip')->nullable(); });
Wenn sich ein Benutzer auf der Website registriert, ben?tige ich nur ein paar Felder, n?mlich 用戶名
、電子郵件地址
、密碼
、名字
和 姓氏
. Dies sind nur Pflichtfelder.
Daher k?nnen die Informationen in der Partnertabelle ausgefüllt werden, nachdem der Benutzer die Website-Registrierung abgeschlossen hat.
Aber aufgrund der Struktur der Fremdschlüssel kann ich aufgrund dieses Fehlers nicht fortfahren:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`mytable`.`tbl_partners`, CONSTRAINT `partners_country_id_foreign` FOREIGN KEY (`country_id`) REFERENCES `tbl_countries` (`id`)) (SQL: insert into `tbl_partners` (`first_name`, `last_name`, `display_name`, `email`, `updated_at`, `created_at`) values (Jack, Wilson, admin, admin@example.com, 2016-06-09 19:41:18, 2016-06-09 19:41:18))
Ich wei?, dass dies an der von der Partnertabelle ben?tigten L?ndertabelle liegt.
Meine Frage lautet: Gibt es eine Problemumgehung, damit ich die Partnertabelle mit dem Land oder anderen nicht erforderlichen Daten füllen kann, aber das externe Tabellenschema für Land, Bundesland usw. beibehalten kann?
對(duì)于 laravel 7.x 創(chuàng)建可為空的外鍵,只需使用:
$table->foreignId('country_id')->nullable()->constrained(); $table->foreignId('state_id')->nullable()->constrained();
記?。?em>可空應(yīng)該之前受約束,否則可空將不會(huì)受到影響。
將 country_id
和 state_id
設(shè)置為可為空,如下所示。
$table->integer('country_id')->nullable()->unsigned(); $table->integer('state_id')->nullable()->unsigned();