I'm trying to make the id start from a certain value. It's working fine when I don't specify the startingValue(1000)
This is my products table schema
Schema::create('products', function (Blueprint $table) {
$table->id()->startingValue(1000);
$table->foreignId('store_id')->constrained()->onDelete('cascade');
$table->foreignId('category_id')->constrained()->onDelete('cascade');
$table->string('name');
$table->float('price');
$table->string('specification');
$table->string('description');
$table->timestamps();
});
This is my favorites table schema
Schema::create('favorites', function (Blueprint $table) {
$table->foreignIdFor(Product::class)->constrained()->onDelete('cascade');
$table->foreignIdFor(User::class)->constrained()->onDelete('cascade');
});
It gives me this error
Thanks 🙏
Related
I am running this migration:
Schema::create('logs', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('key');
$table->text('value')->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
"Users" table migration (migrated at Laravel 5.8)
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('last_message_id')->nullable();
$table->timestamps();
});
Error that I receive:
Foreign key constraint is incorrectly formed
To me it seems like you ran the users migration with a previous version of laravel. Have a look with a db tool (like phpmyadmin, Sequel, etc.) what the actual data type of your users.id field is.
I assume it's not bigInteger so your solution could be to just use
$table->unsignedInteger('user_id');
I want to use two models on my laravel 7.x application : Users and Images :
# Users migration : 2014_10_12_000000_create_users_table.php
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
# Images migration : 2020_03_27_121254_create_models_images_table
Schema::create('images', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id')->unsigned;
$table->string('name');
$table->timestamps();
});
Schema::table('images', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
When trying to migrate i receive following error : General error: 1215 Cannot add foreign key constraint (SQL: alter table images add constraint images_user_id_foreign foreign key (user_id) references users (id) on delete cascade)
I have already searched over google without success, someone could helps me please ?
Thank you
Problem
You are not setting the unsigned properly.
Solution
Replace:
$table->bigInteger('user_id')->unsigned;
By:
$table->bigInteger('user_id')->unsigned();
Explanation
Since the user_id is not unsigned, the definition does not match the id of the users table which means you cannot set the foreign key.
Before Laravel 7.x
You could also do:
$table->unsignedBigInteger('user_id');
Laravel 7.x
As of Laravel 7, you can do it like this in your users migration:
Schema::table('users', function (Blueprint $table) {
$table->id();
// ...
});
And in your images migration:
Schema::table('users', function (Blueprint $table) {
// ...
$table->foreignId('user_id')->constrained();
});
You can find more information here: https://laravel.com/docs/7.x/migrations#foreign-key-constraints
You are missing bracket in unsiged()
As per Laravel Documentation
->unsigned() Set INTEGER columns as UNSIGNED (MySQL)
Change
$table->bigInteger('user_id')->unsigned;
to
$table->bigInteger('user_id')->unsigned();
I have two tables. One is students_attendances and another is students. Now i want to set a foreign key between these two tables. And also I have string data type on which I wanna set foreign key but its giving me the error. Below is my code. Take a review of it ...
//students_attendances Table
public function up()
{
Schema::create('students_attendances', function (Blueprint $table) {
$table->increments('id');
$table->integer('class_id')->index()->unsigned()->nullable();
$table->string('student_id');
$table->string('first_name');
$table->string('last_name');
$table->string('attendance');
$table->timestamps();
$table->foreign('student_id')->references('student_id')->on('students')->onDelete('cascade');
});
}
//students table
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->increments('id');
$table->string('student_id');
$table->string('first_name');
$table->string('last_name');
$table->string('DOB');
$table->integer('students_class_id')->index()->unsigned()->nullable();
$table->string('gender');
$table->string('blood_group');
$table->string('religion');
$table->string('photo_id');
$table->string('student_address');
$table->integer('student_phone_no');
$table->string('guardian_name');
$table->string('guardian_gender');
$table->string('guardian_relation');
$table->string('guardian_occupation');
$table->integer('guardian_phone_no');
$table->string('email')->unique();
$table->string('NIC_no');
$table->string('guardian_address');
// $table->string('document_id');
$table->string('username');
$table->string('password');
$table->timestamps();
});
}
In your students table Change
$table->string('student_id');
to
$table->string('student_id')->unique();
To be used as foreign key, it must be a unique key.
So, the new migration will be
Schema::table('students', function (Blueprint $table) {
$table->unique("student_id");
});
I'm creating a products table in my Laravel application
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('name');
$table->integer('price');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users');
});
}
Afterwards, I'm trying to migrate my database
$ php artisan migrate
However, I'm getting this error when migrating the products table:
Exception trace:
1 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `apps`.`#sql-30d4_61` (errno: 150 "Foreign key constraint is incorrectly formed")")
C:\xampp\htdocs\LTCRUDAUT\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
2 PDOStatement::execute()
C:\xampp\htdocs\LTCRUDAUT\vendor\laravel\framework\src\Illuminate\Database\Connection.php:458
How would I fix it?
This is probably because Laravel now uses bigIncrements() instead of increments() by default for the id field in the user table migration.
For foreign keys, they have to be set to the same field size. To fix:
{
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('user_id')->unsigned();
$table->string('name');
$table->integer('price');
$table->timestamps();
$table->foreign('user_id')
->references('id')
->on('users');
});
}
I have this four tables but I'm getting the above error in Laravel eloquent. can anyone tell what am I missing here? But when I remove the foreign method the migration works fine.
Schema::create('students', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('first_name');
$table->string('last_name');
$table->string('middle_name');
$table->date('birthdate');
$table->integer('degree_id')->unsigned();
$table->index(['id','first_name', 'last_name']);
$table->timestamps();
$table->foreign('degree_id')
->references('id')
->on('degrees');
});
I included unsigned method but still getting the above error.
Schema::create('degrees', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('description');
$table->string('category_id');
$table->integer('duration');
$table->string('sub_code');
$table->timestamps();
$table->foreign('sub_code')
->references('id')
->on('courses');
});
Here's the other two tables.
Schema::create('instructors', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('first_name');
$table->string('middle_name');
$table->string('last_name');
$table->date('birthdate');
$table->index(['id', 'first_name', 'last_name']);
$table->timestamps();
});
And the last table:
Schema::create('courses', function (Blueprint $table) {
$table->string('id')->unique();
$table->string('description');
$table->integer('no_of_units');
$table->string('room_id');
$table->date('schedule');
$table->integer('instructor_id')->unsigned();
$table->timestamps();
$table->foreign('instructor_id')
->references('id')
->on('instructors');
});
The order of your migration files matters. If you're creating the students table first before the degrees table, your foreign key constraint will fail because the degrees table hasn't been created yet.
To solve this, you can move all your foreign key constraints into a migration create_foreign_key_constraints_migration which should run last.