3

I have users and phone table and I made one-to-one relationship in laravel, that's working perfectly but if I try to add data (foreign key user_id) manually to phone table without reference of any user(id), it also work.

In mysql(phpmyadmin), there is no foreign key relation built after the migration.

So I want to ask, what are the advantages of foreign key if it does't put any constraints in db tables or if is there any way to add these constraints using laravel, kindly let me know.

Code snippets

app/Phone.php

public function user(){
    return $this->belongsTo('App\User');
}

app/User.php

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

routes/web.php

Route::get('/', function () {

    $user = factory(\App\User::class)->create();
    $phone=new \App\Phone;

    $phone->phone = '123456789';
    $phone->user_id = '1';
    $user->phone()->save($phone);


});

Phone (migration)

public function up()
{
    Schema::create('phones', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('phone');
        $table->unsignedBigInteger('user_id')->index();
        $table->timestamps();

        $table->foreign('user_id')->references('id')->on('users');
    });
}

Users table does not have any user with id 10 but this also works and add data to phone (user_id)

$phone->phone = '123456789';
$phone->user_id = '10';
$phone->save();
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Sajjad Ali
  • 156
  • 9
  • 1
    is the foreign key constraint properly defined? Does this work by adding data directly through phpmyadmin? – ujjwal verma Dec 20 '19 at 06:22
  • yes. the relation for foreign key is shown above. This is working properly from laravel project but in phpmyadmin, there is no foreign key relation is built and obviously data can be insert from phpmyadmin – Sajjad Ali Dec 20 '19 at 06:28
  • @SajjadAli I think the `foreign_key` is nullable. – TsaiKoga Dec 20 '19 at 06:34
  • I think foreign key is not properly defined as it should not allow setting any user id manually to phone table from phpmyadmin. Try setting the foreign key manually from PHPMyAdmin. – ujjwal verma Dec 20 '19 at 06:34
  • but i need to set foreign key from laravel not manually from phpmyadmin. – Sajjad Ali Dec 20 '19 at 06:36
  • could you please include the phone table migration? – F KIng Dec 20 '19 at 09:07
  • added in question – Sajjad Ali Dec 20 '19 at 11:26
  • Could it be that the table is `MyISAM`? instead of `InnoDB`? – Yoshi Dec 20 '19 at 11:45
  • by default, it is MyISAM. we can change it with Innodb to add foreign key in phpmyadmin but there are no guidelines in laravel for this kind of stuff – Sajjad Ali Dec 20 '19 at 11:48
  • As I have absolutely no knowledge of laravel, I can not give advice here. But as MyISAM does have [fk contraints](https://stackoverflow.com/questions/12971246/why-doesnt-mysqls-myisam-engine-support-foreign-keys), maybe laravel simply ignores them or does not try to create them? So I'd try to instruct laravel to use InnoDB and then check if the situation is changed. – Yoshi Dec 20 '19 at 11:51
  • laravel just builds the one-to-one relationship b/w these two tables (users, phone) in my case with foreign key but does't enforce the constraint to block the invalid/non-referral id to be insert. – Sajjad Ali Dec 20 '19 at 11:55
  • I would've expected as much. I think it's simply too complex to include such checks in the ORM itself, just for the situation where the db does not include the required features. – Yoshi Dec 20 '19 at 11:58

1 Answers1

0

Without the foreign key, there is no point to use a relational database. It helps you to maintain integrity and consistency. It will always check the records in the parent table while inserting into child table, on another hand, it reduces the execution time because of indexes.

In your case, you are able to insert the data into the child table because you did not make the reference key constraints. If you are not using that then you are breaking the role of relational database engines.

It's not a part of any framework or programming language, it's all about right database configuration and design. Laravel is not responsible for it.

There is a disadvantage too but it's only on the machine. It increases the cost of the server CPU.