Heim > Fragen und Antworten > Hauptteil
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?
P粉1357999492023-10-18 13:21:13
对于 laravel 7.x 创建可为空的外键,只需使用:
$table->foreignId('country_id')->nullable()->constrained(); $table->foreignId('state_id')->nullable()->constrained();
记住:可空应该之前受约束,否则可空将不会受到影响。
P粉6383439952023-10-18 09:30:39
将 country_id
和 state_id
设置为可为空,如下所示。
$table->integer('country_id')->nullable()->unsigned(); $table->integer('state_id')->nullable()->unsigned();