Support the ongoing development of Laravel.io →
Configuration Database Eloquent
Last updated 2 years ago.
0

I don't know if what I am going to ask have really something to do with it, but lets go:

Your book_table has a foreign key: author_id, and your author_table have a Foreign key, which is book_id.

Question, why are you linking book_id in the authors table? Since an author can have N books?

Btw, don't you have to tell Eloquent what is the foreign key field? Because you are not following the convention it assumes "book_id" is the PK for the book_table

Last updated 2 years ago.
0

Question, why are you linking book_id in the authors table? Since an author can have N books?

Yes, an author can have N books however each book will have only 1 author. I think that it is okey, isn't ?

Btw, don't you have to tell Eloquent what is the foreign key field? Because you are not following the convention it >assumes "book_id" is the PK for the book_table

How can i say to laravel what is the foreign key ? I think that im saying it when i define each relationship. And i am defining each PK at the beginning of each model:

class Book extends Eloquent implements UserInterface, RemindableInterface {
    protected $table = 'book_table';
   >> protected $primaryKey = 'book_id'; <<
...
...

Can you check if you can use these lanes, please ? thanks.

		$user = Account::find(166); //user


		echo Animals::find(3);

Last updated 2 years ago.
0

Question, why are you linking book_id in the authors table? Since an author can have N books?

Yes, an author can have N books however each book will have only 1 author. I think that it is okey, isn't ?

It is confusing that way, see, this is your authors table, it shouldn't have book_id in it (in my opinion), because you already have author_id into your books table.

If you want to guarantee that a book will have only one author, you should create a UNIQUE index in the books table using book_id and author_id.

Last updated 2 years ago.
0

gabidavila said:

Question, why are you linking book_id in the authors table? Since an author can have N books?

Yes, an author can have N books however each book will have only 1 author. I think that it is okey, isn't ?

It is confusing that way, see, this is your authors table, it shouldn't have book_id in it (in my opinion), because you already have author_id into your books table.

If you want to guarantee that a book will have only one author, you should create a UNIQUE index in the books table using book_id and author_id.

Its true, it is an unnecesary id, i will remove it . However, it doesnt change the problem. Im using HeidiSQL to define PK in my ddbb, can i define a foreign key or it is not possible (noob question) ?

Thanks for you time.

Last updated 2 years ago.
0

Its true, it is an unnecesary id, i will remove it . However, it doesnt change the problem. Im using HeidiSQL to define PK in my ddbb, can i define a foreign key or it is not possible (noob question) ?

Keep in mind that if you are using MySQL, to get foreign keys, you would have to use InnoDB tables.

It is good to have that created because it will make the searches more faster.

To answer your previous question, in your Author model, you should change belongsToMany to hasMany, that way Eloquent will know that there is a FK there.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.