I have 3 models: User , ChangeMoney and CurrencyType, I have simple relationship with User and ChangeMoney and I want to add another relationship with ChangeMoney table with CurrencyType, but I get this error:
D:\xampp\htdocs\epay-pro>php artisan migrate
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `change_money` add constraint change_money_currency_id_foreig
n foreign key (`currency_id`) references `currency_type` (`id`))
current my migration is:
Schema::create('change_money',function(Blueprint $table){
$table->increments('id');
$table->tinyInteger('currency_type');
$table->string('current_money');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('currency');
$table->timestamps();
});
and I don't have any problem, now I want to add other foreign key to CurrencyType table such as:
Schema::create('change_money',function(Blueprint $table){
$table->increments('id');
$table->tinyInteger('currency_type');
$table->string('current_money');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('currency_id')->unsigned();
$table->foreign('currency_id')->references('id')->on('currency_type');
$table->timestamps();
});
currency_type table migration:
Schema::create('currency_type',function(Blueprint $table){
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->string('currency_type','50');
$table->char('currency_symbol','1');
$table->timestamps();
});
Migrations in Laravel runs in order. I don't now your migrations order, but I think that your change_money migration is running first than your currency_type migration. So when the aplication try to add a foreign key in ChangeMoney table to CurrencyType table, the CurrencyType table not exist yet.
Try to create a new migration to add just the relationship between ChangeMoney table and CurrencyType table
You have to remove data from both tables ('change_money', 'currency_type').
Or you can turn off the check of foreign keys. In MySQL, or in the phpmyadmin run this:
SET FOREIGN_KEY_CHECKS=0;
Run migration in Laravel: php artisan migrate
And then again turn on check of foreign keys:
SET FOREIGN_KEY_CHECKS=1;
PROBLEM SOLVED by split foreign key to other migration
class ForeignKeyBetweenChangeMoneyAndCurrencyType extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('change_money', function (Blueprint $table) {
$table->integer('currency_id')->unsigned();
$table->foreign('currency_id')->references('id')->on('currency_type');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('change_money', function(Blueprint $table) {
$table->dropForeign(' change_money_currency_id_foreign');
});
}
}
Related
I'm trying to create foreign keys in Laravel however when I migrate my table using artisan i am thrown the following error:
SQLSTATE[HY000]: General error: 1005 Can't create table `bright`.`carts` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `carts` add constraint `carts_product_id_foreign` foreign key (`product_id`) references `products` (`id`) on delete cascade)
My migration code is as so: carts migration file
public function up()
{
Schema::create('carts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('product_id')->unsigned();
$table->integer('customer_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->foreign('customer_id')->references('id')->on('customers')->onDelete('cascade');
$table->string('quantity');
});
}
products migration file
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('unit');
$table->decimal('price', 6, 3);
$table->string('img_url');
$table->string('description');
$table->integer('sold');
$table->integer('in_stock');
});
}
customers migration file
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('first_name');
$table->string('last_name');
$table->string('phone_number');
$table->string('address');
$table->string('zip')->nullable();
$table->string('email')->unique();
$table->string('user_name')->unique();
$table->string('password');
});
}
Any ideas as to what I've done wrong, I want to get this right now, as I've got a lot of tables I need to create e.g. carts, products, customers. Ideally I want to create carts table which hold this data with the foreign keys, i..e product_id and cart_id
Try using this structure.
$table->foreignIdFor(Product::class);
$table->foreign('product_id')->references('id')->on('products');
$table->foreignIdFor(Customer::class);
$table->foreign('customer_id')->references('id')->on('customers');
Also make sure, your products and customers migrations run before your carts migrations, and your models are imported to your migrations.
You can use like this.
$table->unsignedBigInteger('product_id')->nullable();
$table->foreign('product_id')->references('id')->on('products');
$table->unsignedBigInteger('customer_id')->nullable();
$table->foreign('customer_id')->references('id')->on('customers');
Am working on a Laravel 5.6 application whereby I have 2 tables mainly sponsors table and children table. Am creating a one to many relationship between the tables before migrating them. A child may have many sponsors.
The problem is I get this error when migrating them using php artisan migrate command:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error:
1005 Can't create table larangular.#sql-520c_21f (errno: 150
"Foreign key constraint is incorrectly formed") (SQL: alter table
sponsors add constraint sponsors_child_id_foreign foreign key
(child_id) references children (id) on delete cascade)
Sponsors migration
public function up()
{
Schema::create('sponsors', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('child_id')->unsignedInteger();
$table->foreign('child_id')->references('id')->on('children')->onDelete('cascade');
$table->foreign('child_id')->references('id')->on('children');
$table->string('email')->unique();
$table->string('phone');
$table->string('nationality');
$table->timestamps();
});
}
Children migration
public function up()
{
Schema::create('children', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('age');
$table->string('gender');
$table->timestamps();
});
}
Make sure that your children migration runs before your sponsors migration, and then the column should be this:
$table->unsignedInteger('child_id');
// or
$table->integer('child_id')->unsigned();
unsignedInteger is a function that creates a column, you should not call it on an existing column.
// This creates an unsigned integer column.
$table->unsignedInteger('child_id');
I'm trying to rollback my database, but have this error:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table tb_levels)
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails
this is my migration code:
public function up()
{
Schema::disableForeignKeyConstraints();
Schema::create('tb_users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id_user');
$table->string('name');
$table->string('username');
$table->string('email')->unique();
$table->integer('id_level')->unsigned();
$table->string('password', 60);
$table->rememberToken();
$table->boolean('activated')->default(false);
$table->timestamps();
$table->foreign('id_level')->references('id_level')->on('tb_levels');
});
Schema::enableForeignKeyConstraints();
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::table('tb_users', function(Blueprint $table){
$table->dropForeign('tb_users_id_level_foreign');
$table->dropColumn('id_level');
});
Schema::drop('tb_users');
Schema::enableForeignKeyConstraints();
}
I've tried several ways that I found in this forum, but still got that error, any help please?
First Disable Foreign Key using this:
SET FOREIGN_KEY_CHECKS=1;
SET GLOBAL FOREIGN_KEY_CHECKS=1;
And then migrate your database.
Again Apply Foreign key constrain:
SET FOREIGN_KEY_CHECKS=0;
SET GLOBAL FOREIGN_KEY_CHECKS=0;
Well, finally I found a way to solve that error,
First of all you need to make a migration to drop the foreign key and column, this is the code:
public function up()
{
Schema::disableForeignKeyConstraints();
Schema::table('tb_users', function(Blueprint $table){
$table->dropForeign('tb_users_id_level_foreign');
$table->dropColumn('id_level');
});
Schema::enableForeignKeyConstraints();
}
Then migrate it, after that It will drop the column and foreign key.
After that delete Schema::table code, save it, and run command:
php artisan migrate:reset
Well, It works but it's really an impractical way,
Hope that out there had more easy way rather than this.
I am trying to learn one-to-one relations. I am using Laravel 5.5.13.
My simple app is this:
I create a App\Message. I optionally can associate a App\Task with it.
My goal:
Once a App\Task is associated, if the task is deleted, it should cascade delete the App\Message row.
And the reverse, if the App\Message is deleted, it should cascade and delete the App\Task row.
However I am having an error on migrate because the tasks_table is created AFTER the messages_table.
Here is my migration:
2017_10_15_021803_create_messages_table.php:
public function up()
{
Schema::create('messages', function (Blueprint $table) {
$table->increments('id');
$table->integer('task_id')->unsigned()->nullable();
$table->text('body');
$table->timestamps();
$table->foreign('task_id')->references('id')->on('tasks')->onDelete('cascade'); //// IF I COMMENT THIS OUT THE MIGRATION WORKS, BUT I NEED THIS IN
});
}
And for App\Task 2017_10_15_023343_create_tasks_table.php:
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->integer('message_id')->unsigned();
$table->timestamps();
$table->foreign('message_id')->references('id')->on('messages')->onDelete('cascade');
});
}
If I comment out the $table->foreign('task_id')->references('id')->on('tasks')->onDelete('cascade'); then the migration works, BUT i need the deletion of the message when the task is deleted, and without this line that won't happen.
The error I get after running php artisan migrate is:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table petfolk.#sql-42e8_16f (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table messages add constraint messages_task_id_foreign foreign key (task_id) references tasks (id) on delete set null)
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table petfolk.#sql-42e8_16f (errno: 150 "Foreign key constraint is incorrectly formed")
Just create third migration and move foreign key adding logic in the migration:
Schema::table('messages', function (Blueprint $table) {
$table->foreign('task_id')->references('id')->on('tasks')->onDelete('cascade');
}
It will work when both tables will be created.
A one-to-one relationship links one row in a database table to one (and only one) row in another table.
onDelete('cascade') is used when there is an intermediate, also known as a pivot, table between two tables in a many-to-many relationship. You do not need this for a one-to-one relationship because the link to the messages table only exists in the row of the tasks table (and that is being deleted).
Try this for the tasks table migration:
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->integer('message_id')->unsigned()->nullable();
$table->foreign('message_id')->references('id')->on('messages');
$table->timestamps();
});
}
The Problem
I want to add foreign keys to tables. When I run my first migration create_posts_table that looks like this:
Schema::create('posts', function(Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->unsignedInteger('user_id')->index();
// . . .
});
Schema::table('posts', function(Blueprint $table)
{
$table->foreign('user_id')->references('id')
->on('users')->onDelete('cascade');
});
The following error is thrown:
[Illuminate\Database\QueryException] SQLSTATE[HY000]:
General error: 1215 Cannot add foreign key constraint (SQL: alter table
posts add constraint posts_user_id_foreign foreign key (user_id) references users (id) on delete cascade)
This is caused because the users table is not created yet, hence the failure to create the users' referencing foreign key on the posts table.
Possible Solution
The solution to this problem would be to add the foreign keys with a new migration after all of the tables had been created. However, it seems clunky to me.
The question
How can I define the foreign keys inside their respective tables' migrations, instead of adding them separately with the different migration after all of the tables had been created?
You can perform multiple migrations in the same migration file. If you have a posts table where you want a foreign key to the users table, but the users table does not yet exist, you either have to do it in the users table migration file after the users table has been created - or you have to do a separate migration, like you said. You can't "save" instructions for later in migrations.
In laravel way is keeping separate migration files for different tables with indexing, primary key & foreign keys.....
CreateUsersTable
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('email');
$table->string('password', 60);
$table->enum('status', ['0', '1'])->default('0');
$table->rememberToken();
$table->nullableTimestamps();
$table->unique('email');
});
}
public function down()
{
Schema::drop('users');
}
}
CreatePostsTable
class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
}
public function down()
{
Schema::drop('posts');
}
}