errno150 cant create table migration - laravel-5

this my code schema goimon
public function up()
{
Schema::create('goimon', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->charset = 'utf8';
$table->collation = 'utf8_unicode_ci';
$table->increments('MaGoiMon');
$table->string('TinhTrang');
//$table->unsignedInteger('MaNhanVien');
$table->integer('MaNhanVien')->unsigned();
$table->unsignedInteger('MaBan');
$table->foreign('MaBan')->references('MaBan')->on('banan');
$table->foreign('MaNhanVien')->references('MaNhanVien')->on('nhanvien');
$table->timestamps();
});
}
and second schema :
public function up()
{
Schema::create('nhanvien', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('MaNhanVien');
$table->string('TenNhanVien');
$table->string('TenDangNhap');
$table->string('MatKhau');
$table->boolean('GioiTinh');
$table->date('NgaySinh');
$table->integer('CMND');
$table->integer('quyen');
$table->timestamps();
});
}
and my error
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error:
1005 Can't create table `qlnhahang`.`#sql-12bc_
491` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL:
alter table `goimon` add constraint `goimon_manhanvien_foreign`
foreign key (`MaNhanVien`) references `nhanvien` (`MaNhanVien`))

Related

How to add foreign key in laravel migration?

I have an employer_profiles table, a job_posts table and an employer_profile_job_post table. I'm trying to make a relationship and add the foreign keys but I keep getting this error and I'm not sure why.
SQLSTATE[HY000]: General error: 1005 Can't create table `highrjobsadminlte`.`employer_profile_job_post` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `employer_profile_job_post` add constraint `employer_profile_job_post_job_post_id_foreign` foreign key (`job_post_id`) references `job_posts` (`id`))
create_employer_profile_job_post_table.php:
public function up()
{
Schema::create('employer_profile_job_post', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('employer_profile_id')->unsigned()->index();
$table->bigInteger('job_post_id')->unsigned()->index();
$table->timestamps();
$table->foreign('employer_profile_id')->references('id')->on('employer_profiles');
$table->foreign('job_post_id')->references('id')->on('job_posts');
});
}
create_employer_profiles_table.php:
public function up()
{
Schema::create('employer_profiles', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->index()->unsigned();
$table->string('company_name');
$table->string('phone',30)->nullable();
$table->string('street');
$table->string('city', 50);
$table->string('country', 80);
$table->string('zip_postal', 25);
$table->string('province_state', 50);
$table->string('number_of_employees');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
create_job_posts_table.php:
public function up()
{
Schema::create('job_posts', function (Blueprint $table) {
$table->id();
$table->bigInteger('user_id')->index()->unsigned();
$table->bigInteger('employer_profile_id')->index()->unsigned();
$table->string('job_title');
$table->longText('job_description');
$table->string('salary', 20);
$table->string('employment_type', 20)->nullable();
$table->string('location_name', 100);
$table->tinyInteger('is_active')->default(0);
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('employer_profile_id')->references('id')->on('employer_profiles');
});
}

“Foreign key constraint is incorrectly formed” (errno 150) : Laravel migration

When I migrate my DB, I got this error. Below is my coding:
Code:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('category_id')->nullable();
$table->string('name');
$table->text('description')->nullable();
$table->boolean('active')->default(1)->nullable();
$table->double('code');
$table->integer('unit')->default(1);
$table->string('image')->nullable();
$table->double('price');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('category_id')->references('id')->on('categories');
});
}
Error:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table reochi.products (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table products add constraint products_category_id_foreign foreign key (category_id) references categories (id))

Laravel 5.8 error SQLSTATE[HY000]: General error: 1005 uuid

I am using Laravel 5.8 and package "goldspecdigital/laravel-eloquent-uuid" because I need to use UUID4 and here is my migration file:
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->bigIncrements('id');
// $table->timestamps();
$table->string('path');
$table->uuid('visit_id');
$table->foreign('visit_id')->references('id')->on('visits');
});
}
I get the following error:
SQLSTATE[HY000]: General error: 1005 Can't create table doctors
_pharmacy.images (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table images
add constraint images_visit_id_foreign foreign key (visit_id) references visits (id))
How do I solve this?
Update the schema for visits and images as follows.
Then run php artisan migrate cmd.
visits table schema
public function up()
{
Schema::create('visits', function (Blueprint $table) {
$table->uuid('id')->primary();
// your column will be here
......
......
$table->timestamps();
});
}
images table schema
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('path');
$table->uuid('visit_id');
$table->foreign('visit_id')->references('id')->on('visits');
});
}

laravel migration "SQL: alter table `posts` add constraint `posts_category_id_foreign` foreign key (`category_id`) references `categories` (`id`) "

am trying to do laravel migration but am getting this error
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table `urbancruise`.`#sql-18f8_112` (errno: 150 "Foreign key cons
traint is incorrectly formed") (SQL: alter table `posts` add constraint `posts_category_id_foreign` foreign key (`c
ategory_id`) references `categories` (`id`))
here is my code
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
public function down(){
Schema::drop('categories');
}
public function up(){
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories');
$table->timestamps();
});
}
public function down(){
Schema::drop('posts');
}
When you are creating a foreign key, the categories.id and posts.category_id must have the same type.
Swap the ->bigInteger() with ->integer()should solve your problem:
Your categories migration:
public function up(){
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
public function down(){
Schema::drop('categories');
}
In your posts migration:
public function up(){
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id', false, true);
//Or $table->unsignedInteger('category_id');
$table->foreign('category_id')
->references('id')
->on('categories');
$table->timestamps();
});
}
public function down(){
Schema::drop('posts');
}
Hope it helps.
Foreign key should always be of same type in both child and parent table.
In your case, categories.id is of type ٰINTEGER while posts.category_id is defined as BIGINT. Replacing your posts migration with below should work.
public function up() {
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->foreign('category_id')->references('id')->on('categories');
$table->timestamps();
});
}

errno: 150 "Foreign key constraint is incorrectly formed" - bigIncrements

I got this error when I change I to bigIncrements:
.`products` (errno: 150 "Foreign key constraint is incorrectly formed")
my code:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->string('title');
$table->integer('current_buy');
$table->integer('count');
$table->text('short_description');
$table->text('long_description');
$table->tinyInteger('status')->default(0);
$table->string('series');
$table->integer('max_buy');
$table->integer('parent_product_id')->nullable()->unsigned();
$table->foreign('parent_product_id')->references('id')->on('products')->onDelete('cascade');
$table->tinyInteger('admin_seen')->default(0);
$table->timestamps();
});
DB::update("ALTER TABLE products AUTO_INCREMENT = 1000;");
}
but below code works fine;
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->integer('current_buy');
$table->integer('count');
$table->text('short_description');
$table->text('long_description');
$table->tinyInteger('status')->default(0);
$table->string('series');
$table->integer('max_buy');
$table->integer('parent_product_id')->nullable()->unsigned();
$table->foreign('parent_product_id')->references('id')->on('products')->onDelete('cascade');
$table->tinyInteger('admin_seen')->default(0);
$table->timestamps();
});
// DB::update("ALTER TABLE products AUTO_INCREMENT = 1000;");
}
Since you're using bigIncrements() for id, to make it work change this:
$table->integer('parent_product_id')->nullable()->unsigned();
to this:
$table->bigInteger('parent_product_id')->nullable()->unsigned();

Resources