Laravel cannot add foreign key - php

Something broke in my migrations file as I am doing a migrate:fresh and it throws:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
(SQL: create table `product` (`test` int not null) default character
set utf8mb4 collate utf8mb4_unicode_ci)
I have deleted everything in that migration and left only one field in order to debug what may be the issue, and here is the migration as is now and the one which is giving me this error:
public function up()
{
Schema::create('product', function (Blueprint $table) {
$table->integer('test');
});
}
I tried doing composer dump-autoload but nothing changed.
EDIT
After doing vagrant halt and vagrant up the first error I'm getting after migrate:freshis:
SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write;
duplicate key in table '#sql-4ef_4' (SQL: alter table
`product_shipping_country` add constraint `fk_psc_product` foreign key
(`product_id`) references `product` (`content_id`) on delete CASCADE on
update CASCADE)
But immediately after I run migrate:fresh this error is no longer present, and I am getting the old one again
EDIT 2
I tried manually deleting the DB, same thing happens. Also clearing both composer clear-cache and artisan cache:clear
EDIT 3
If I delete that migration, and every migration after that one, the error is gone, so I suppose the error is in that specific migration?
I don't get how can I get foreign-key error when I even don't have foreign keys in the migration??

It was some cache issue obviously. I resolved it by completely deleting the DB and creating it again. Migrations and reseeds worked perfectly after that.

Related

How to migrate my users table in Laravel after I made a change?

I am building an eCommerce site and I am running into an issue when migrating my orders table. I am getting the following error:
SQLSTATE[HY000]: General error: 3780 Referencing column 'user_id' and referenced column 'id' in foreign key constraint 'orders_user_id_foreign' are incompatible. (SQL: alter table `orders` add constraint `orders_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete set null on update cascade)
The schema that sets up the foreign key for this migration looks like:
// set up foreign key on users
$table->integer('user_id')->unsigned()->nullable(); // will be null if guest
$table->foreign('user_id')
->references('id')->on('users')
->onUpdate('cascade')
->onDelete('set null');
I figured out that the users table schema sets up the id like so:
$table->id();
And the orders table schema sets up the id like this:
$table->increments('id');
So I changed the users migration so that it is the same as the orders migration i.e both are like:
$table->increments('id');
But when I run php artisan migrate, I get this back:
➜ MobileMastery_V2 git:(master) ✗ php artisan migrate --path=/database/migrations/2014_10_12_000000_create_users_table.php
Nothing to migrate.
As you can see above I tried declaring the path to the migration I wanted to see if that would work but it didn't. How can I get this single file to migrate? Thanks
Laravel migrations are intended to keep track of the changes you make on your database over time. Laravel creates a table called migrations that keeps track of the migrations if it already ran or not. So in this case you can create a new migration that only makes that change, or if your database has no data on it yet you can drop the database schema, make your modifications on the migrations and run it again.
And about the primary key and foreign keys, I recommend you to read this part of Laravel documentation.
You should create migration for this change and run php artisan migrate.

make "Integrity constraint violation: 1048 Column cannot be null " into an alert message

i want to stop the Integrity constraint violation:
1048 Column 'adresse' cannot be null
error message from showing up and turn it into an alert or some error page and make a return to home screen button or smth
i'm a newbie to laravel and phpmyadmin but i had a similar problem with an already exist message.
That's because 'addresse' is not nullable. You can update this by either changing the table or editing this line in the existing migration file and refresh the database (tip: you lose all data if you do that)
To change your table without deleting any data and considering the table name is users:
php artisan make:migration updateUsersAddresseFieldTable
Schema::table('users', function (Blueprint $table) {
$table->string('adresse')->nullable()->change();
});
php artisan migrate
If you want to adapt the existing migration file and refresh:
$table->string('adresse')->nullable();
php artisan migrate:refresh

Column 'tax_id' cannot be NOT NULL in Laravel

I am working in a project where a table has a foreign key called tax_id, the problem is that in some point another migration was created to change the tax_id to nullable.
Schema::table('products', function (Blueprint $table) {
$table->unsignedInteger('tax_id')->nullable()->change();
});
I realize this after I wrote other migrations and tried to run them, it throws Column 'tax_id' cannot be NOT NULL, it seems that in the "project" that migration was run, so I can't just delete the file, how can I run my migrations without the error? I have tried to remove the foreign key, but nothing worked.
I'll assume you're working with MySQL InnoDB because you did not specify the database type/engine.
You could try different approaches:
1) If yuo're in hurry, disable foreign keys, do the update, enable the foreign key. You should then correct the problem of course.
2) Add set tax_id to '' where tax_id is null and then do the migration
3) Document yourself about the database engine and implement a correct solution
Giacomo

How to drop foreign key using Laravel migrations?

I've tried to drop a an InnoDB table that holds a foreign key using Laravel Migrations but I found out that I need to drop the foreign first but what I've read on the doc and on articles doesn't work.
Here's the portion of the code creating the problem :
Schema::table('admin_admin_action', function(Blueprint $table) {
$table->dropForeign(['admin_action_id']);
$table->dropColumn('admin_action_id');
$table->dropForeign(['admin_id']);
$table->dropColumn('admin_id');
});
And here's the error code
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'admin_admin_action_admin_action_id_foreign'; check that column/key exists (SQL: alter table `admin_admin_action` drop foreign key `admin_admin_action_admin_action_id_foreign`)
This table is the result of a many to many association and here are the foreign keys
Schema::table('admin_admin_action', function($table) {
$table->unsignedInteger('admin_id');
$table->unsignedInteger('admin_action_id');
$table->foreign('admin_id')->references('id')->on('admin');
$table->foreign('admin_action_id')->references('id')->on('admin_actions');
});
I've also tried specifying the full foreign key name but it says that the key/column doesn't exist though it does exist in the database.
I'm doubting this is a migration's issue because the migration runs smoothly but that's not the case for the rollback.
Edit: The foreign keys are respectively named
admin_admin_action_admin_id_foreign
admin_admin_action_admin_action_id_foreign
Help!
I found the solution !
You're right ! It's a mis-conception issue I've created a "admin_admin_action" and an "admin_action_admin" tables. So I've been trying to the table that doesn't have the foreign keys but has the same position in the database. They're duplicated ! Thanks

Foreign Key contraints with AWS RDS not working

I'm trying to move my local laravel database up to RDS, I have had lots of problems with Elastic beanstalk so I decided to go a different way. When I migrate I get the following error with every foreign key I have:
[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error:
1215 Cannot add foreign key constraint (SQL: alter table videos add
constraint videos_object_id_foreign foreign key (object_id)
references objects (id) on delete cascade)
PDOException
[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
I have looked into RDS's foreign key constraints and they say they are not enforced, but it says they can be used for informational purposes.
Every foreign key is unsigned.
Schema::create('videos', function (Blueprint $table) {
$table->increments('id');
$table->integer('object_id')->unsigned()->nullable();
$table->foreign('object_id')->references('id')->on('objects')->onDelete('cascade');
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->rememberToken();
$table->timestamps();
});
Note: I have also tried separated the forein keys into a separate migration after the table was created and had no luck.
To find the specific error run this:
SHOW ENGINE INNODB STATUS
And look in the LATEST FOREIGN KEY ERROR section.
The data type for the child column must match the parent column exactly.
Also, you should run the query set foreign_key_checks=0 before running the DDL so you can create the tables in an arbitrary order rather than needing to create all parent tables before the relevant child tables.

Categories