Laravel 数据库模式,可以为空的外部

新手上路,请多包涵

我有这两个数据库表:

  1. 用户表
  2. 合作伙伴表

用户表 将处理此类信息

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

合作伙伴表 将包含所有用户元信息,例如名字和姓氏等。

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

When a user is register to the site I only want few fields which are, username , email address , password , first name and last name 。这些只是必填字段。

因此,合作伙伴表中的信息可以在用户完成对站点的注册后填写。

但是由于外键的结构,由于此错误,我无法继续进行:

 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))

我知道这是由合作伙伴表所需的国家表引起的。

我的问题是:是否有解决方法,以便我可以在合作伙伴表上填写国家或任何其他不需要的数据,但保留国家、州等的外部表模式。

原文由 nonsensecreativity 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 297
2 个回答

设置 country_idstate_id 为空,像这样。

 $table->integer('country_id')->nullable()->unsigned();

$table->integer('state_id')->nullable()->unsigned();

原文由 rcx1 发布,翻译遵循 CC BY-SA 3.0 许可协议

为了让 laravel 7.x 创建一个可为空的外键,只需简单地使用:

 $table->foreignId('country_id')->nullable()->constrained();

$table->foreignId('state_id')->nullable()->constrained();

记住: nullable 应该 约束 之前,否则 nullable 不会受到影响。

原文由 moghwan 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题