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');
});
}
Related
I have a project named Partner
I need to create developers table and two other tables branches and projects
And every Developer has many branches and has many projects
So I created this migration files:
Developers
public function up()
{
Schema::create('developers', function (Blueprint $table) {
$table->id();
$table->string('company_name', 100);
$table->string('responsible_name', 100);
$table->string('email', 100);
$table->string('phone', 100);
$table->string('hotline', 100)->nullable();
$table->timestamps();
});
}
Branches
public function up()
{
Schema::create('branches', function (Blueprint $table) {
$table->id();
$table->integer('developer_id')->unsigned();
$table->string('name', 100);
$table->string('address', 100);
$table->string('location', 100);
$table->string('phone', 100);
$table->timestamps();
$table->foreign('developer_id')->references('id')->on('developers')->onDelete('cascade');
});
}
Projects
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->id();
$table->integer('developer_id')->unsigned();
$table->string('name', 100);
$table->string('type', 100);
$table->string('address', 100);
$table->string('location', 100);
$table->string('availability', 100);
$table->text('payment_plan');
$table->string('brochure', 100)->nullable();
$table->timestamps();
$table->foreign('developer_id')->references('id')->on('developers')->onDelete('cascade');
});
}
But when I ran php artisan migrate I got this error
SQLSTATE[HY000]: General error: 1005 Can't create table `partner`.`branches` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `branches` add constraint `branches_developer_id_foreign` foreign key (`developer_id`) references `developers` (`id`) on delete cascade)
What is the wrong with the form of the foreign key?
You can use foreign key in migration without predefine field :
$table->foreignId('developer_id')->constrained('developers')->onDelete('cascade');
and remove this line :
$table->integer('developer_id')->unsigned();
I keep getting an error when trying to do a database migration
SQLSTATE[42703]: Undefined column: 7 ERROR: column "user_id" referenced in foreign key constraint does not exist (SQL: alter table "ads" add constraint "ads_user_id_foreign" foreign key ("user_id") references "users" ("id"))
This is my migration file
public function up()
{
Schema::create('ads', function (Blueprint $table) {
$table->id();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('category_id')->references('id')->on('categories');
$table->foreign('city_id')->references('id')->on('cities');
$table->string('title');
$table->text('description');
$table->text('photos');
$table->string('status');
$table->timestamps();
});
}
You need first to create the columns before add foreign key
public function up()
{
Schema::create('ads', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('category_id');
$table->unsignedBigInteger('city_id');
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('category_id')->references('id')->on('categories');
$table->foreign('city_id')->references('id')->on('cities');
$table->string('title');
$table->text('description');
$table->text('photos');
$table->string('status');
$table->timestamps();
});
}
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();
});
}
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`))
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();