【问题标题】:Laravel Database Schema, Nullable ForeignLaravel 数据库模式,可以为空的外部
【发布时间】:2016-10-10 15:26:28
【问题描述】:

我有这两个数据库表:

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

Partner Tables将包含所有用户元信息,例如名字和姓氏等。

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

当用户注册到站点时,我只需要几个字段,usernameemail addresspasswordfirst namelast 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))

我知道这是由于合作伙伴表所需的国家/地区表造成的。
我的问题是:是否有解决方法,以便我可以在合作伙伴表上填写国家或任何其他不需要的数据,但保留国家、州等的外部表架构。

【问题讨论】:

    标签: php mysql database laravel


    【解决方案1】:

    让 laravel 7.x 创建一个可以为空的外键只需使用:

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

    记住:nullable 应该是 before constrained 否则 nullable 不会受到影响。

    【讨论】:

    • 确实有特殊的行为。我也很感兴趣为什么 nullable() 应该排在第一位。
    • 我浪费了一个小时的工作,因为我不知道在约束之前必须编写可空值。
    • 您好,您来了,又为我节省了一天。谢谢,陌生人。
    • 但是nullable()为什么要放在第一位呢?
    【解决方案2】:

    设置country_idstate_id 可以为空,像这样。

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

    【讨论】:

    • 如果在创建schema后需要更改字段可为空,那么该行应该是$table->integer('country_id')->nullable()->unsigned()->change() ;
    • 非常好我的朋友
    • 如何定义nullable()->unsigned() 字段的关系?这也需要$table->foreign('country_id')->references('id')->on('countries') 吗?
    【解决方案3】:

    使用最新版本的 Laravel,你可以将nullable 方法与foreignKey 结合使用:

    $table
          ->foreignId('other_table_id')
          ->nullable() // here
          ->references('id')
          ->on('other_table');
    

    【讨论】:

    • 如果父表上的外来 id 是 uuid,我该如何添加?
    • @theodory 类似:$table->foreignUuid('business_id')->nullable()->references('id')->on('businesses')->onDelete('cascade')->onUpdate('cascade');
    【解决方案4】:

    对于 Laravel 7.x,我使用这种方式:

    $table->bigInteger('word_type_id')->nullable()->unsigned();
    $table->index('word_type_id')->nullable();
    $table->foreign('word_type_id')->nullable()->references('id')->on('word_types')->onDelete('cascade');
    

    【讨论】:

      猜你喜欢
      • 2019-11-15
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 1970-01-01
      • 2018-07-14
      • 1970-01-01
      相关资源
      最近更新 更多