How to use foreignUuid in laravel 9 - laravel

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

Related

Foreign key constraint is incorrectly formed laravel 7

parcel table
$table->unsignedBigInteger('shop_id');
$table->foreign('shop_id')->references('id')->on('shops');
** I have a model name Shop I want to add its is to id as a foreign key to parcel table **
I think that the problem lies in the order you created your migrations. Look at the order and make sure that shops table comes before the parcels table. If this is not the case, then the easiest way would be to change the table times e.g. date_time_name. Change time and you'll be set.

i am trying to create foreign key constraint in 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.

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.

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

PHPMyADMIN - mysql show foreign key constraints #1216 - Magento 2 Database

On localhost i export a magento 2 database using phpmyadmin and try to upload on our server but it giving us error of #1216 - Foreign key constraints. The problem is TABLE A define first in sql and try to create Table B field as Foreign key. So if TABLE B is not creating then giving Foreign key error and not creating database. When i am doing same using command prompt it is working fine. So using phpmyadmin through the error ? Can any one please help me on it?
To solve this use the checkbox Disable Foreign Key Checks when exporting from PhpMyadmin. Or set it yourself:
SET FOREIGN_KEY_CHECKS=0;
and at the end:
SET FOREIGN_KEY_CHECKS=1;

Resources