i am trying to create foreign key constraint in laravel 5.8 - laravel-5.8

I can't create a foreign table key:
$table->increments('rt_id');
$table->integer('issued_id')->unsigned();
$table->foreign('issued_id')->references('issue_id')->on('book_issues');
$table->integer('book_id')->unsigned();
$table->foreign('book_id')->references('id')->on('book_details');
I also tried this:
$table->bigIncrements('rt_id');
$table->unsignedBigInteger('issued_id');
$table->foreign('issued_id')->references('issue_id')->on('book_issues');
$table->unsignedBigInteger('book_id');
$table->foreign('book_id')->references('id')->on('book_details');

Looking both of your tables I figured it out that in order to get successful foriegn key... the referenced and referencing fields must have exactly the same data type and options .
So in your case you should remove unsignedBigInteger and unsigned just use integer like this:
$table->integer('issued_id')
Let me know if this worked for you

I just rechecked all the fields and the datatype, i observed that i used increments for primary key and in foreign key i had taken integer('book_id')->unsigned()
And while migrating, i already had the primary table migrated so, i used php artisan migrate:fresh command then this time, it is migrating the foreign table time so, i'm getting the error.
before my foreign key table is been placed first and then the primary key table is placed at last.
So, i changed my model name and replaced according to key constraints as primary table should be stored first in migration folder even sometimes while migrating it doesn't migrates in sequences in ur folder.
So, i used php artisan migrate:fresh
This command is migrating all tables in sequences.

Related

How to use foreignUuid in laravel 9

so im trying to use UUID instead of id in my table
i have two tables, in both, I am using UUID to generate an id. after that, I am trying to use one id as a foreign in the second table.
Im using laravel 9
here my first table
and here is my second table
but when im trying to run php artisan migrate it show this error
i already trying to find on internet but still got nothing
laravel 7 using as uuid foreign key
Foreign key constraint when your primary key is UUID type column
thanks..
thanks, i think i solved it
here's the way, i change my first migration like this
and my second migration

eloquent migration does not follow the name order

i go some foreign keys problems
so i deleted all the tables in DB and changed the migrations names so i get this arrangement:
but i always got thie error in table 10... because it does not follow the name order
(errno: 150 "Foreign key constraint is incorrectly formed")
The migrations does not follow the names order but the timestamp on the file. So change the time on the file the first part 7_04_.. to whichever comes next of the one you are trying to add a foreign key to. The field and table have to exist in order for the foreign key constraint to work.
When Laravel generates a migration the first part is the current timestamp, and then it follows the name of your migration, so please use that, there is no need to manually edit the file names of the migrations.

Unable to delete a record when created foreign-key constraints manually in mysql without using migration.

I have created database tables manually and i have also created foreign key constraints in tables manually without using migration. now when i am going to delete a record it giving me following error-
Integrity constraint violation: Cannot delete or update a parent row:
a foreign key constraint fails
Note: i can't use migration because database was already created.
Use ON DELETE CASCADE and ON UPDATE CASCADE on foreign keys, like:
...(create/alter Children table query)...
CONSTRAINT FK_ParentChild
FOREIGN KEY (parent_id) REFERENCES Parents(id)
ON UPDATE CASCADE ON DELETE CASCADE;
So you don't have to manually delete children before deleting the parent element. All children records will automatically delete along with the deletion of parent record.
I would suggest investigating this further by doing the follow:
On your development environment, write migrations to create the
tables from scratch 2)
Look at the generated schema and compare it
to the schema of your existing production tables
Note the
differences between the two schema and write migrations to correct
the production tables.
Note:
If you need to correct the data in the meantime and you are confident that the data integrity of your database is not at risk. You can use the following statements to drop the foreign key checks whilst you correct the data.
SET FOREIGN_KEY_CHECKS = 0;
SET FOREIGN_KEY_CHECKS = 1;
I can't stress enough, remember to turn on your foreign key checks once you are finished and do not do this on a production database. Take a copy of the database and try things locally until you are confident that your corrections are 100% safe ;)

How to get database error when i try to delete a row that has its key like foreign key in another table?

(Laravel framework)
How to create tables in database with migrations so when I try to delete a row from a table that has its key as a foreign key somewhere in another table, I got an error from database not letting me to do that ?
Do I have to create relations in migrations where I say what are the foreign keys in my database, or is there another way using only Laravel models.
I am new with this. Thank you.
You need to remove deactivate the foreign key check so to say, I am using following function to accomplish that. First I set the Foreign key check to 0 then truncate the table and set it back to 1. Setting the foreign key check to 0 allows one to truncate the table even if there are foreign keys.
# functions to truncate users table even if there are foreign key
public static function truncateUserTable()
{
DB::statement('SET FOREIGN_KEY_CHECKS=0');
User::query()->truncate();
DB::statement('SET FOREIGN_KEY_CHECKS=1');
}

Laravel unique foreign key mysql

I have a laravel application with a mysql database
I need to make a unique query, where more than one column in the table is unique with another causing unique rows. I want to include a foreign key in this.
How do I do this?
I have tried doing
$ table -> unique etc but I can't get the foreign key working
I am getting an error saying that the foreign key doesn't exist.
Can anyone please help

Resources