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
Related
I get the following error when I try to add a foreign key to my purchaseorderheader table :
ORA-02270: "no matching unique or primary key for this column-list"
I made sure the table and column names are the same but I still get the error, any ideas?
You have a composite primary key that is composed of two columns but, in your foreign key, you are only referencing one of the columns of the composite primary key and not the entire primary key.
You either need to:
change the primary key to be only on the single column purchaseorderid;
keep the existing primary key and additionally create a UNIQUE key on only the purchaseorderid column of the first table; or
include both columns of the primary key in the foreign key.
Whichever option you choose, you should make sure that it implements the business logic that you are trying to capture in the table; if it does not then you should revisit the database-design and find a design that does capture your business logic.
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
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.
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 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');
}