3

I'm creating a website with Laravel for the first time. I checked relationships documentation today and it seems that Laravel just uses simple SQL queries.

class User extends Eloquent {

    public function phone()
    {
        return $this->hasOne('Phone');
    }

}

$phone = User::find(1)->phone;

And it's basically a select * from phones where user_id = 1 query. So the question is, should I use foreign keys anyway in my database to create relationships?

  • Isn't user_id a foreign key? – JeffO Jan 5 '15 at 22:21
5

Foreign keys in your database enables data integrity, as you can't delete a parent row if there is a child row in another table.

While you can rely on the framework to handle data for you, the framework will not enable data integrity and you will eventually end up with orphan rows in your database.

So, my advice is: design the database properly in order to preserve data.

  • Just to extend: having put FK constraints on your database is an invisible change, except in two possible cases: when you insert/update an FK that doesn't exist, or when your delete action orphans some rows (assuming you don't cascade delete). Both of these are erroneous situations where you usually want things to throw an error and refuse to continue, so having FKs that throw errors is desirable. A well designed system that doesn't make any FK errors when it doesn't have FK constraints will never notice that FK constraints have been put on the database. – Flater Aug 30 at 13:56
4

The data belongs to the organization, not the application. While right now you're using Laravel exclusively, in the future there might be several applications interacting with the database.

I'm currently working on an app that started in FoxPro, migrated to ASP classic, and was partially upgraded to ASP.NET. There are at least three applications interacting with the data and we're working on integrating with more. Our database doesn't use foreign keys, although it does sort of use triggers to emulate them, and I really wish it did.

  • +100000000 for "The data belongs to the organization, not the application. " – Tulains Córdova Jul 8 '16 at 22:02
  • @TulainsCórdova Full disclosure, I read that from here, but I didn't want to link it since it's not really related. – Andrew says Reinstate Monica Jul 8 '16 at 22:04
2

Using the exact same words from @JeffO:

Isn't user_id a foreign key?

I think the problem is not necessarily setting a foreign key or not. The problem is how much do you need to depend on the database features.

In Laravel you will set your foreign keys while adding your migrations.

But if you for any reason/error have a user having direct access to the database or using PHP features without the framework or whatever other issue the might not only rely on the framework, it is best practice to add foreign keys to your database.

Finally, I already worked on projects where they decided not to have foreign keys and rely only on the frameworks validations for maintenance reasons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.