sourcecode

Laravel 데이터베이스 스키마, Nullable Foreign

copyscript 2022. 10. 7. 22:23
반응형

Laravel 데이터베이스 스키마, Nullable Foreign

다음 두 개의 데이터베이스 테이블이 있습니다.

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

사용자가 사이트에 등록되면 다음과 같은 몇 개의 필드만 필요합니다.username,email address,password,first name그리고.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))

파트너 테이블에서 요구하는 국가 테이블이 원인인 것으로 알고 있습니다.
질문입니다. 파트너 테이블에 국가나 기타 불필요한 데이터를 기입할 수 있지만 국가나 주 등에 대한 외부 테이블 스키마는 유지할 수 있는 작업이 있습니까?

larabel 7.x에서 늘 가능한 외부 키를 작성하려면 다음 절차를 따릅니다.

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

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

주의: nullable은 구속되기 전에 해야 합니다.그렇지 않으면 nullable에는 영향이 없습니다.

설정country_id및 그state_id무효, 이렇게.

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

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

최신 버전의 Larabel을 사용하면nullable병용 방법foreignKey:

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

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

라라벨 8

$table->foreignId('foreign_key_id')->nullable()->constrained()->onDelete('cascade');

참조('{column}'->on('{table}')이 있는 경우 nullable()->constrainted()를 다음 예 앞에 붙여야 합니다.

$table->foreignId('author')->nullable()->constrained()->references('id')->on('users');

이것은 라라벨 9에 있지만 아마 아까도 있을 것이다.

언급URL : https://stackoverflow.com/questions/37735055/laravel-database-schema-nullable-foreign

반응형