2013-06-04 11 views
5

Mam kilka migracji w nowym projekcie Laravel 4. Jedna dotyczy regionów, a druga obszarów. Każdy region ma wiele obszarów, a obszary należą do regionów.Laravel 4 Migracje rzucające 1072 błąd

Użyłem Laravel 4 i funkcji migracji przy wielu okazjach, ale nigdy wcześniej nie spotkałem się z tym problemem. Kiedy uruchamiam php artisan migrate:install następnie php artisan migrate pojawia się następujący błąd:

$ php artisan migrate 
    [Exception] 
    SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'region_ 
    id' doesn't exist in table (SQL: alter table `areas` add constraint areas_r 
    egion_id_foreign foreign key (`region_id`) references `regions` (`id`)) (Bi 
    ndings: array (
)) 
migrate [--bench[="..."]] [--database[="..."]] [--path[="..."]] [--package[="... 
"]] [--pretend] [--seed] 

// Migracja regiony

class CreateRegionsTable extends Migration { 

/** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
    // Creates the regions table 
    Schema::create('regions', function($table) 
    { 
     $table->engine = 'InnoDB'; 
     $table->increments('id'); 
     $table->string('name', 160)->unique(); 
     $table->timestamps(); 
    }); 
    } 
} 

// Migracja obszary

class CreateAreasTable extends Migration { 

/** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
    // Creates the cemeteries table 
    Schema::create('areas', function($table) 
    { 
     $table->engine = 'InnoDB'; 
     $table->increments('id'); 
     $table->foreign('region_id')->references('id')->on('regions'); 
     $table->string('name', 160)->unique(); 
     $table->timestamps(); 
    }); 
    } 
} 

Odpowiedz

17

Trzeba utworzyć kolumnę związane z kluczem zagranicznym:

class CreateAreasTable extends Migration { 

/** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
    // Creates the cemeteries table 
    Schema::create('areas', function($table) 
    { 
     $table->engine = 'InnoDB'; 
     $table->increments('id'); 

     $table->integer('region_id')->unsigned(); 
     $table->foreign('region_id')->references('id')->on('regions'); 

     $table->string('name', 160)->unique(); 
     $table->timestamps(); 

    }); 
    } 
} 

Czasami (zależy od serwera bazy danych) musisz utworzyć kluczy obcych w dwóch etapach:

class CreateAreasTable extends Migration { 

/** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
    // Create the table and the foreign key column 
    Schema::create('areas', function($table) 
    { 
     $table->engine = 'InnoDB'; 
     $table->increments('id'); 

     $table->integer('region_id')->unsigned(); 

     $table->string('name', 160)->unique(); 
     $table->timestamps(); 

    }); 

    // Create the relation 
    Schema::tabe('areas', function($table) 
    { 
     $table->foreign('region_id')->references('id')->on('regions'); 
    }); 
    } 
} 
+0

Ah, to interesujące. Więc $ table-> foreign ('columnname') ... nie tworzy kolumny opartej na liczbach całkowitych, jeśli jeszcze nie istnieje? – JasonMortonNZ

+0

To wszystko. Po prostu tworzy relację między tabelami. –

+0

To rozwiązuje błąd, który otrzymałem, ale zastąpiło go innym. [Wyjątek] SQLSTATE [HY000]: Błąd ogólny: 1005 Nie można utworzyć tabelę 'NZF-org # sql-107c_ c8' (errno: 150) (SQL: alter table 'areas' dodać ograniczenie areas_region_id_f oreign klucz obcy ('region_id') odnosi się do' regionów' ("id")) (Wiązania: ar ray ( )) – JasonMortonNZ

4

i nie zapomnij, aby klucze obce niepodpisany.

  $table->integer('region_id')->unsigned(); 
+0

Dlaczego muszę niepodpisać moich kluczy zagranicznych? Co to oznacza i jakie są korzyści? – JasonMortonNZ

+3

Jest to rada laravel doc: Uwaga: Podczas tworzenia klucz obcy odwołujący się wzrastających całkowitą, należy pamiętać, aby zawsze zrobić kolumnę klucza obcego niepodpisane klucze podstawowe nie może być ujemna więc kluczowym wskazując im obce powinny być również niepodpisany. Pozwala to również podwoić liczbę kluczy, które możesz mieć w tej kolumnie. –