外部キーに問題があります。Laravel "外部キー制約を追加できません" - 移行
マイ移行ユーザー:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('nom');
$table->string('prenom');
$table->string('adresse');
$table->integer('cp');
$table->string('ville');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->tinyInteger('admin')->nullable();
});
Schema::table('users', function ($table) {
$table->integer('sexe_id')->unsigned();
$table->foreign('sexe_id')->references('id')->on('sexes');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}`
マイsexeの移行:
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sexes', function (Blueprint $table) {
$table->increments('id');
$table->string('libelle');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('sexes');
}
マイ男女のシーダ:
class SexesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('sexes')->insert([
[
'libelle' => 'Homme',
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
],
[
'libelle' => 'Femme',
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]
]);
}
}
マイユーザーシーダ 私はセックスを持っているユーザーがいます:
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
[
'name' => 'admin',
'nom' => 'Virlois',
'prenom' => 'Peter',
'sexe_id' => 1,
'email' => '[email protected]',
'adresse' => '12 rue Jean Rostand',
'cp' => 90000,
'ville' => 'Belfort',
'password' => bcrypt('admin123'),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
'admin' => 1,
],
[
'name' => 'test',
'nom' => 'Mennegain',
'prenom' => 'Mathieu',
'sexe_id' => 1,
'email' => '[email protected]',
'adresse' => '12 rue Jean Rostand',
'cp' => 90000,
'ville' => 'Belfort',
'password' => bcrypt('test123'),
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
'admin' => 0,
]
]);
}
}
マイデータベースシーダ:
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call(SexesTableSeeder::class);
$this->call(UsersTableSeeder::class);
$this->call(SaisonTableSeeder::class);
$this->call(ProduitTypeSeeder::class);
$this->call(SportsTableSeeder::class);
$this->call(ProduitsTableSeeder::class);
}
}
私が実行:PHPの職人の移行を:リフレッシュは、私はこのエラーを持っている
を-seed:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `users` add constraint `users_sexe_id_foreign` foreign key (`
sexe_id`) references `sexes` (`id`))
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
データベースエンジンが 'InnoDB'であることを確認します。' MyISAM'は外部キーをサポートしていません。 – shoieb0101