Benzer bir sorun yaşıyorum. Bu yüzden ayrı başlık açkmak istemedim.
Şöyle bir migration sınıfım var.
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBaseTables extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function($table) {
$table->increments('id');
$table->string('name', 100)->unique();
$table->string('password');
$table->tinyInteger('level')->default(3);
$table->string('mail')->unique();
$table->tinyInteger('enable')->default(0);
$table->timestamps();
$table->index('name');
});
Schema::create('corps', function($table) {
$table->increments('id');
$table->string('name', 255)->nullable();
$table->tinyInteger('enable')->nullable();
$table->string('country', 60)->nullable();
$table->string('city', 60)->nullable();
$table->string('area_name', 70)->nullable();
$table->string('address', 255)->nullable();
$table->string('tel', 17)->nullable();
$table->string('te2', 17)->nullable();
$table->string('fax', 17)->nullable();
$table->string('corpmail', 255);
$table->string('corp_url', 200);
$table->timestamps();
$table->unique('name');
$table->index('name');
});
Schema::create('userprof', function($table) {
$table->increments('id');
$table->integer('user_id'); // This should be an index!!!
$table->string('name', 150)->nullable();
$table->string('surname', 150)->nullable();
$table->integer('corp_id')->unsigned()->nullable(); // This should be an index!!!
$table->string('about')->nullable();
$table->string('tel', 17)->nullable();
$table->string('country', 60)->nullable();
$table->string('city', 60)->nullable();
$table->smallInteger('zip_code')->nullable()->unsigned;
$table->string('area_name', 70)->nullable();
$table->string('address', 255)->nullable();
$table->string('tel2', 17)->nullable();
$table->string('fax', 17)->nullable();
$table->timestamps();
$table->index('user_id');
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('corp_id')->references('id')->on('corps');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
Schema::drop('corps');
Schema::drop('userprof');
}
}
Ne yaptıysam şu foreign key ekleyemedim.
Önce her tablo için birer sınıf eklemiştim. Sonrasında foreign key hatasını çözemedim Veritabanında tablo açma sırasından emin olmak adına tek sınıftan bu tabloları eklemeye çalıştım. Ama yine foreign key hatası aldım.
4-5 saattir nette araştırma yapıyorum. Çözüm önerilerini denedim. Hiçbiri işe yaramadı.
Şunları da denedim ve kontrol ettim,
A Summary of the answers already listed, plus mine:
Foreign Keys generally require InnoDb, so set your default engine, or explicitly specify
$table->engine = 'InnoDB';
Foreign keys require the referenced table to exist. Make sure the referenced table is created in an earlier migration, prior to creating the key. Consider creating the keys in a separate migration to be sure.
Foreign Keys require the data type to be congruent. Check whether the referenced field is the same type, whether its signed or unsigned, whether it's length is the same (or less).
If you are switching between hand coding migrations, and using generators, make sure you check the id type you are using. Artisan uses increments() by default but Jeffrey Way appears to prefer integer('id', true).
kaynak: http://stackoverflow.com/questions/11894250/general-error-1005-cant-create-table-using-laravel-schema-build-and-foreign/18914911#18914911
şu hatada takılıp duruyorum:
SQLSTATE[HY000]: General error: 1005 Can't create table 'laravelplastport.#sql-45a_2c1' (errno: 150) (SQL: alter table `userprof` add const
raint userprof_user_id_foreign foreign key (`user_id`) references `users` (`id`))