I have seen many people write migrations without defining foreign key constraints including jeffrey way in his videos. For example,
Schema::create('post',function(Blueprint $table){
$table->id();
$table->integer('user_id')->index();
$table->text('body');
//
});
Here, there is no foreign key constraint defined like the following
$table->foreign('user_id')->references('id')->on('users');
I always define foreign key because that's how you maintain the relationships and integrity between tables. But I have seen many videos where they do not define the foreign key. Is there any specific reason behind it?
Foreign key constraints make your database consistent. I don't know any another way to do something like cascading. You should read about this too. Consider a scenario where you delete the parent element and child element is just sitting there in some related table. huh! Now that can break your app.
PS: It's not necessary but SHOULD be done.
It's not necessary but we should index everything we use on where(). So yes, you should cretae foreign keys, this way you create indexes and link the tables.
The only reasons to not use these keys are if you don't need consistency in your database or you are sure that your framework/code is not prone to these referential errors. But as seen in the videos, the foreign keys do not have to be defined to have functioning relations in your laravel app.
Related
While designing the database for a laravel software using MYSQL, is assigning foreign keys relevant or does Laravel take care of that "Software side".
In the migration we have something like
Table Example:
$table->unsignedBigInteger('user_id');
should i modify the example table in phpmyadmin and make user_id a foreign key? is there a better way or is this not relevant or necessary?
You should define foreign key constraints in your migration. When using code base you should make all the changes using migrations.
Additionally by defining foreign key you actually build a relation between 2 tables otherwise this relation will be at code level. When relation is built database will restrict to have only values which actually exists in main table. Using foreign key you can also do cascading (on update and delete) at db level.
Reference what are the advantages of defining a foreign key
Why should I use foreign keys in database?
if use Laravel 7 ,you can use this short that is a column name user_id foreign to ID user in the table users :
$table->foreignId('user_id')->nullable()->constrained()->onDelete('cascade');
I have question about making relation between two tables in mysql. I create table with column which is foreign key, but I dont use foreign key references keyword. I connect tables in code(php/asp.net). I dont know if it is good habit? Thanks for your help.
It's generally seen as a good habit to create a foreign key constraint as it will enforce data integrity between the two columns.
Yes, you can have 100% valid data in your database without using any foreign key constraints at all, but implementing them will make it impossible for a flawed update, delete, or insert to violate the foreign constraint between them in the future.
At past, I was used to make a table relationship programmatically, which is quite handy since you don't need to make FK constraint to each table which have relation.
But, I wonder what is the differences or the advantages of giving a FK constraint to tables that have relation, instead of just creating an attribute and retrieve them programmatically (calls the tables where field = another table PK).
Just some information, I work on php independent MVC framework without any dependency to eloquent or something else.
Hope someone give me some short lesson on this :D Thank you and have a nice day!
There are certain principles that you should follow while coding and development, I can say that there is no issue whether or not you create a foreign key constraint to a table that has relation or not but you know that won't restrict the column to have only those values that are being referenced by it. So basically it is not a good DB Schema and may lead to inconsistencies. For example deleting a parent table's row you will have to manually delete the child table's row on the other hand if you have a foreign key constraint that to onDelete = cascade, your database will automatically take care of everything and there won't any inconsistencies.
I get that foreign key constraints great for integrity of a database and all, but it's also a huge overhead to use constraints when dealing with tables that are in the millions and growing.
I want to remove foreign key constraints from my application. In past symfony2 projects I've removed constraints manually, but I'm assuming maybe there is a way to simply tell symfony2 to do this?
If anyone is aware of a way to do this within the framework please let me know :)
edit:
Let's say for example in a manyToMany relationship, it auto-creates the relationship table with the proper indexes but it also puts a foreign key constraint on both columns as well, or if i have a oneToMany relationship it puts a foreign key constraint on that relationship.
I don't want these foreign key constraints to be created.
Found in Doctrine's JIRA:
You can disable the exporting of foreign keys for specific models:
User:
attributes:
export: tables
columns:
or with php:
$userTable->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_TABLES);
Now it will only export the table definition and none of the foreign keys. You can use: none, tables, constraints, plugins, or all.
This is for a sort of proof of concept draft to get things working, but don't want to have completely crap code. For my database, I tried to get true foreign key relations going using innoDB, but couldn't get it.
Instead of using foreign keys, I decided to just pull mysql_insert_id() after inserts, saving it as a variable, then putting that variable into the related table.
Is this horrible? Everything seems to work well, and I'm able to connect and relate ID's as needed. What benefits would using foreign keys give me over my method (besides updates/deletes cascading)?
To create a relation (master->detail), you have to always supply the keys by yourself, either using mysql_insert_id, natural keys or key generated by your applications. The FOREIGN KEY is not going to make that work for you.
What FOREIGN KEY does is
Helping you enforce the relationship/the integrity of your data (so the "detail" record does not point to an invalid parent)
Handles deletion or key alterations of master records (ON DELETE ..., ON UPDATE ...).
It's also creating an index in your "detail"-table for the "master_id"-row if it doesn't exist yet (okay, you could also do that without FOREIGN KEY)
Has also some kind of documenting purpose for example an ERM-tool could reengineer the relationship model from your schema (okay, this point is a slight long shot)
The cost of adding the FOREIGN KEY constraint statement is small compared to its benefits.