2016-06-09 16 views
7

Sono queste tabelle di due database:laravel Database Schema, Nullable Esteri

  1. tabelle utente
  2. Partner Tavoli

tabelle utente gestirà questo tipo di informazioni

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

Mentre Tabella partner es volontà contiene tutte le meta informazioni dell'utente come nome e cognome, ecc

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

Quando un utente è registrarsi al sito voglio soltanto pochi settori che, username, email address, password, first name e last name. Questi sono solo i campi obbligatori.

Quindi le informazioni nelle tabelle partner possono essere compilate in seguito dopo che l'utente ha terminato la registrazione al sito.

Ma a causa della struttura della chiave esterna, non posso procedere oltre a causa di questo errore:

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, [email protected], 2016-06-09 19:41:18, 2016-06-09 19:41:18)) 

So che questo è causa di tabella paesi che è richiesto dalla tabella partner.
La mia domanda è: esiste un modo per aggirare in modo da poter riempire il paese o altri dati non necessari sul tavolo di partner, ma mantenere lo schema della tabella straniero per i paesi, afferma, ecc

risposta

23

Impostare la country_id e la state_id annullabile , così.

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

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

Se è necessario cambiare l'annullabile campo dopo la creazione dello schema, poi la linea dovrebbe essere $ tavola-> intero ('country_id') -> nullable() -> senza segno() -> modifica (); – sh6210