I am trying to make two tables relationship in laravel. But i can't understand why i'm getting error "errno: 150 "Foreign key constraint is incorrectly formed"". please check my migration code below.
items table
public function up()
{
Schema::create('items', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->string('name');
$table->decimal('price',5,2);
$table->integer('count_left')->default(0);
$table->timestamps();
$table->foreign('category_id')->references('id')->on('categories');
});
}
categories
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
}
Related
In my Larave-8, I created a table using this migration:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id');
$table->string('first_name',50);
$table->string('last_name',50);
$table->string('other_name',50)->nullable();
$table->string('gender',20);
$table->string('user_photo',350)->nullable();
$table->integer('nationality_id');
$table->string('marital_status',50);
$table->date('dob')->nullable();
$table->string('address', 300)->nullable();
$table->integer('country_id')->nullable();
$table->integer('state_id')->nullable();
$table->integer('city_id')->nullable();
$table->string('cv_file',350)->nullable();
$table->text('summary')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
How do I alter the table through migration and add these:
$table->foreign('nationality_id')->references('id')->on('countries');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('state_id')->references('id')->on('state_origins');
$table->foreign('city_id')->references('id')->on('cities');
Thanks
create another migration like to alter existing table
public function up()
{
Schema::table('profiles', function (Blueprint $table) {
$table->foreign('nationality_id')->references('id')->on('countries');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('state_id')->references('id')->on('state_origins');
$table->foreign('city_id')->references('id')->on('cities');
});
}
public function down() {
Schema::table('profiles', function (Blueprint $table) {
$table->dropForeign('nationality_id');
$table->dropForeign('country_id');
$table->dropForeign('state_id');
$table->dropForeign('city_id');
});
}
I am trying to make a laravel migration & establishing an foreign key constrain between two tables.
Here is some of my codes:
create_user_education_table
class CreateUserEducationTable extends Migration
{
public function up()
{
Schema::create('user_education', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')
->references('id')
->on('users')
->onUpdate('CASCADE')
->onDelete('CASCADE');
$table->unsignedBigInteger('institution_id');
$table->foreign('institution_id')
->references('id')
->on('education_institutions')
->onUpdate('CASCADE')
->onDelete('CASCADE');
$table->string('degree')->nullable();
$table->string('grade')->nullable();
$table->year('start_year')->nullable();
$table->year("end_year")->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::table('user_education', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['institution_id']);
});
Schema::dropIfExists('user_education');
}
}
create_educational_institution_table
class CreateEducationInstitutionsTable extends Migration
{
public function up()
{
Schema::create('education_institutions', function (Blueprint $table) {
$table->id();
$table->string('institution_name');
$table->string('country');
$table->string('city');
$table->string('logo')->nullable();
$table->text('description')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('education_institutions');
}
}
while migrating ```user_education_table`` it throwing following error.
What's the point here ?
you must create education_institutions table before of creating user_education table
I want to add a foreign key to my schools table but the primary key of the provinces table is not added to the foreign key reference.
This is my Schools Table
public function up()
{
Schema::create('schools', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("name");
$table->unsignedTinyInteger("province_id");
});
}
and Provinces Table
public function up()
{
Schema::create('provinces', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("name",255);
});
}
and this is my Foreign key constraint
public function up()
{
Schema::table('schools', function (Blueprint $table) {
$table->foreign('province_id')->reference('id')->on('provinces');
});
}
and this is the error
enter image description here
If there is a solution tell me.
Thanks.
Try this and also check the sequence of your migrations.
public function up()
{
Schema::create('student_profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("name",64);
$table->string("father_name",64);
$table->string("last_name",64);
$table->string("grand_father_name",64);
$table->string("graduation_year",4)->unique();
$table->enum("gender",['0','1']);
$table->unsignedSmallInteger('school_id')->nullable();
$table->unsignedTinyInteger("province_id")->nullable();
$table->foreign('school_id')->reference('id')->on('schools')-
>onDelete('set null');
$table->foreign('province_id')->reference('id')->on('provinces')->onDelete('set null');
});
}
public function up()
{
Schema::table('schools', function (Blueprint $table) {
$table->unsignedBigInteger('province_id')->nullable();
$table->foreign('province_id')->reference('id')->on('provinces')
->onDelete('set null');
});
}
I've been researching this for hours, can't seem to solve it. Here is the error
General error: 1215 Cannot add foreign key constraint (SQL: alter table 'users' add constraint 'users_discord_id_foreign' foreign key ('discord_id') references 'discord_o_auths' ('id'))
Here is my DiscordOAuths Migration:
public function up()
{
Schema::create('discord_o_auths', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->integer('id')->unique();
$table->string('access_token')->unique();
$table->string('refresh_token')->unique();
$table->bigInteger('token_expiration');
$table->timestamps();
});
}
And here is my Users Migration.
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->id();
$table->boolean('admin')->default(false);
$table->integer('color_scheme')->default(0);
$table->rememberToken();
$table->timestamps();
});
Schema::table('users', function($table) {
$table->engine = "InnoDB";
$table->integer('discord_id')->unsigned();
$table->foreign('discord_id')->references('id')->on('discord_o_auths');
});
}
The DiscordOAuths table is created before the Users table as well. What am I doing wrong?
You're trying to assign a foreign key constraint to an integer column, which is not the same as the unsigned columns that Laravel uses for its id's.
In your DiscordOAuths you have to replace $table->integer('id')->unique(); with $table->bigIncrements('id'); or the new method added in Laravel 7 $table->id(); which is an alias of the previous one.
Your discord_id column in the users migration also needs to be an unsigned column. You have to define it using either $table->unsignedBigInteger('discord_id'); or its alias that came with Laravel 7: $table->foreignId('discord_id');
So your migrations would look like this:
For DiscordOAuths
public function up()
{
Schema::create('discord_o_auths', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->id();
$table->string('access_token')->unique();
$table->string('refresh_token')->unique();
$table->bigInteger('token_expiration');
$table->timestamps();
});
}
And for users it would be like this:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->engine = "InnoDB";
$table->id();
$table->boolean('admin')->default(false);
$table->integer('color_scheme')->default(0);
$table->rememberToken();
$table->timestamps();
});
Schema::table('users', function($table) {
$table->engine = "InnoDB";
$table->foreignId('discord_id');
$table->foreign('discord_id')->references('id')->on('discord_o_auths');
});
}
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();
});
}