Laravel Database Schema,Nullable Foreign

non*_*ity 17 php mysql database 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();
});
Run Code Online (Sandbox Code Playgroud)

虽然合作伙伴表将包含所有用户的元信息,如姓氏和名字等.

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();
});
Run Code Online (Sandbox Code Playgroud)

当用户注册到网站,我只想要几个字段它们是,username,email address,password,first 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))
Run Code Online (Sandbox Code Playgroud)

我知道这是由伙伴表所需的countries表引起的.
我的问题是:是否有一个解决方案,所以我可以填写合作伙伴表上的国家或任何其他非必需数据,但保留国家,州等的外国表架构.

mog*_*wan 46

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

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

$table->foreignId('state_id')->nullable()->constrained();
Run Code Online (Sandbox Code Playgroud)

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

  • 我浪费了一小时的工作,因为我不知道 nullable 必须先写在 constrained 之前。 (9认同)
  • 确实很奇怪的行为。我也很感兴趣为什么 `nullable()` 应该放在第一位。 (6认同)
  • 它应该是第一位的,因为这就是它的实现方式。另外,如果你从逻辑上考虑一下,如果你尝试约束然后说可空,这就像试图挡住子弹,它被发射了,然后你穿上凯夫拉尔背心。 (2认同)

小智 43

设置country_idstate_id可空,就像这样.

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

$table->integer('state_id')->nullable()->unsigned();
Run Code Online (Sandbox Code Playgroud)

  • 如果你需要在创建模式后更改字段为nullable,那么该行应该是$ table-> integer('country_id') - > nullable() - > unsigned() - > change(); (13认同)
  • 如何定义“nullable()->unsigned()”字段的关系?还需要这个 ```$table->foreign('country_id')->references('id')->on('countries')``` 吗? (2认同)

bob*_*ito 14

使用最新版本的 Laravel,您可以nullable结合使用该方法foreignKey

$table
      ->foreignId('other_table_id')
      ->nullable() // here
      ->references('id')
      ->on('other_table');
Run Code Online (Sandbox Code Playgroud)


Pay*_*jad 6

对于 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');
Run Code Online (Sandbox Code Playgroud)


Cra*_*eld 6

请注意,如果您有references('{column}')->on('{table}'),则需要在此之前放置 nullable()->constrained() ,即:

$table->foreignId('author')->nullable()->constrained()->references('id')->on('users');
Run Code Online (Sandbox Code Playgroud)

这是 Laravel 9 上的内容,但可能也更早。