I'm trying to make a foreign key constraint using laravel migration
This my first table
public function up()
{
Schema::create('postal_codes', function (Blueprint $table) {
$table->char('id', 5)->primary();
$table->string('district', 150);
$table->string('city', 150);
$table->integer('province_id')->unsigned();
});
Schema::table('postal_codes', function ($table) {
$table->foreign('province_id')->references('id')->on('provinces');
});
}
And this is my second table
public function up()
{
Schema::create('postal_code_details', function (Blueprint $table) {
$table->id();
$table->string('urban', 150);
$table->char('postal_code', 5);
});
Schema::table('postal_code_details', function ($table) {
$table->foreign('postal_code', 5)->references('id')->on('postal_codes');
});
}
when i run
php artisan migrate:fresh
get error something like this :
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table
postal_code_detailsadd constraint5foreign key (postal_code) referencespostal_codes(id))
Im using mySQL for database What wrong with the code?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire