W
W
WeReng2016-09-04 11:22:07
Server equipment
WeReng, 2016-09-04 11:22:07

Do I need foreign keys in the database when developing on laravel?

Hello! Recently I began to study Laravel, I am writing my simple application. When designing the database, the question arose of whether it is necessary to specify relationships for tables at the database level, i.e. specify foreign keys for tables. As I understand it, laravel models can also specify all the necessary links.
The main question is, is there a need to specify links at the database level, will this become a problem in the future, or vice versa, will it save a lot of problems?
If you still need to specify foreign keys in the database, help determine what actions to put on ON DELETE or ON UPDATE, for example, for the users and posts tables.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Ulanov, 2016-07-08
@antonsr98

my advice to you is not to keep a file washer on a machine with 1c databases ... this can play a cruel joke with the database

M
Muhammad, 2016-09-04
@WeReng

Yes, you need to specify.
On the contrary, they will help to avoid problems. You can, of course, manually prescribe all this, but why?
Depends on the type of connection.
Example:
Profile - user settings (let's assume that they are stored in a separate table). Without a user, the settings are meaningless, so they also need to be removed when deleting the user.
Blog posts - user. There is already a choice:
1. You can prohibit deleting a user if he has at least one post
2. Delete all posts of the user along with the account

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question