Closed mgarimelHES closed 2 years ago
Hi @mgarimelHES -
The data type for _bookid should be the same data type used for id in the books table, since that's the corresponding value it will hold.
So let's take a closer look at the id field in the books table...
It's created using the id method, which we're told is just an alias for bigIncrements.
From the docs we see that bigIncrements creates a field of the data type BIGINT.
So to match this for our _bookid field in the reviews table, we should use the method bigInteger.
I just corrected my example/solution to reflect this, as I mistakenly used integer instead of bigInteger.
@mgarimelHES The work you are completing with relational fields is fascinating.
Like Susan stated the field type of the foreign table reference needs to match that of the primary field being referred to and as specific as if integer then it can't be of a larger or smaller size referenced (ie: tinyInt or bigInt).
Also, I have found it useful to make sure in my migration that the field I am referencing from another table has a foreign key reference that I use the foreign()->references()->on().
I have noted that if we follow conventions it makes things easier as you don't need to explicitly reference all of the details with our relationships, meaning that if you have setup your primary keys to reference a standard "id" field then the foreign reference is assumed to be
$table->integer('contact_id')->unsigned();
$table->foreign('contact_id')->references('id')->on('contacts');
#
Hi Susan,
In my practice for 'Reviews' table for the week8, I have done the following - (please note the field 'book_id' ) -
Schema::create('reviews', function (Blueprint $table) { $table->id(); $table->timestamps(); $table->string('book_id'); $table->string('first_name'); $table->string('last_name'); $table->tinyInteger('rating'); $table->boolean('recommended'); $table->text('content'); });
Q - Is it ok if I assume 'book_id' as an alpha-numeric value instead of an integer?
Thanks
Murthy
This will help me too. Thanks
@mgarimelHES The work you are completing with relational fields is fascinating.
Like Susan stated the field type of the foreign table reference needs to match that of the primary field being referred to and as specific as if integer then it can't be of a larger or smaller size referenced (ie: tinyInt or bigInt).
Also, I have found it useful to make sure in my migration that the field I am referencing from another table has a foreign key reference that I use the foreign()->references()->on().
I have noted that if we follow conventions it makes things easier as you don't need to explicitly reference all of the details with our relationships, meaning that if you have setup your primary keys to reference a standard "id" field then the foreign reference is assumed to be _id.
$table->integer('contact_id')->unsigned(); $table->foreign('contact_id')->references('id')->on('contacts');
Hi @YvaGithub - Thanks for your reply, I think I may have to wait for the 'Relationships' lesson . appreciate your help.
Hi Susan,
In my practice for 'Reviews' table for the week8, I have done the following - (please note the field 'book_id' ) -
Schema::create('reviews', function (Blueprint $table) { $table->id(); $table->timestamps(); $table->string('book_id'); $table->string('first_name'); $table->string('last_name'); $table->tinyInteger('rating'); $table->boolean('recommended'); $table->text('content'); });
Q - Is it ok if I assume 'book_id' as an alpha-numeric value instead of an integer?
Thanks
Murthy