in Education by
I'm trying to add foreign key in my table exam: public function up() { Schema::create('exams', function (Blueprint $table) { $table->id(); $table->BigInteger('student_id')->unsigned()->nullable(); $table->BigInteger('subject_id')->unsigned()->nullable(); $table->integer('mark'); $table->timestamps(); $table->foreign('student_id') ->references('id') ->on('students'); $table->foreign('subject_id') ->references('id') ->on('subjects'); }); } Student's table: public function up() { Schema::create('students', function (Blueprint $table) { $table->id(); $table->string('fname'); $table->string('mname'); $table->string('lname'); $table->timestamps(); }); } Subject's table: Schema::create('subjects', function (Blueprint $table) { $table->id(); $table->string('title'); $table->timestamps(); }); I have searched a lot and try it with unsignedBigInteger or $table->bigInteger('student_id')->unsigned()->nullable(); and try this: Laravel migration: "Foreign key constraint is incorrectly formed" (errno 150) But this error is still happening when I use php artisan migrate What can i do? Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1005 Can't create table salamstu.exams (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table exams add constraint exams_student_id_foreign foreign key (student_id) references students (id) on delete cascade) at C:\Users\User\Documents\laravel\SalamStu\vendor\laravel\framework\src\Illuminate\Database\Connection.php:716 712▕ // If an exception occurs when attempting to run a query, we'll format the error 713▕ // message to include the bindings with SQL, which will make this exception a 714▕ // lot more helpful to the developer instead of just the database's errors. 715▕ catch (Exception $e) { ➜ 716▕ throw new QueryException( 717▕ $query, $this->prepareBindings($bindings), $e 718▕ ); 719▕ } 720▕ } 1 C:\Users\User\Documents\laravel\SalamStu\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501 PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table salamstu.exams (errno: 150 "Foreign key constraint is incorrectly formed")") 2 C:\Users\User\Documents\laravel\SalamStu\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501 PDOStatement::execute() JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
$table->bigIncrements('id'); = Auto-incrementing UNSIGNED BIGINT (primary key) equivalent column. $table->increments('id'); = Auto-incrementing UNSIGNED INTEGER (primary key) equivalent column. INT[(M)] [UNSIGNED] [ZEROFILL] A normal-size integer. The signed range is -2147483648 to 2147483647. The unsigned range is 0 to 4294967295. BIGINT[(M)] [UNSIGNED] [ZEROFILL] A large integer. The signed range is -9223372036854775808 to 9223372036854775807. The unsigned range is 0 to 18446744073709551615. For $table->increments('id');, use this: $table->unsignedInteger('student_id')->nullable(); $table->foreign('student_id')->references('id')->on('students'); For $table->bigIncrements('id');, use this: $table->unsignedBigInteger('student_id')->nullable(); $table->foreign('student_id')->references('id')->on('students');

Related questions

0 votes
    Closed. This question needs to be more focused. It is not currently accepting answers. Want to improve this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    I am using Laravel 5.7 version. I got the below error for users_activations table while run the command ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I am using Laravel 5.7 version. I got the below error for users_activations table while run the command ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    I have the following models: class Project(models.Model): name = models.CharField(max_length=300, unique= ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
0 votes
    I'm using EF code first. By convention, I have added a foreign key and a reference to the foreign ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    Suggestions needed for creating better and efficient search indexes for models having foreign key and many-to-many ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 4, 2022 in Education by JackTerrance
0 votes
    5. Which key defines a relationship between two tables? (a) Primary key (b) Foreign key (b) Secondary key (d) ... Full form of RDBMS is Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    What is the correct definition of foreign key? The common field of two tables. A field that contains unique data ... of another table. Select the correct answer from above options...
asked Dec 15, 2021 in Education by JackTerrance
0 votes
    What is the correct definition of foreign key? The common field of two tables. A field that contains unique data ... of another table. Select the correct answer from above options...
asked Dec 15, 2021 in Education by JackTerrance
0 votes
    In the above DDL command the foreign key entries are got by using the keyword (a) References (b) ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    In the section relation which of the following is used as a foreign key? (a) Course_id (b) ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    How can we temporarily disable a foreign key constraint in MySQL?...
asked Feb 1, 2021 in Technology by JackTerrance
0 votes
0 votes
    How to Add Your SSH Key in Gerrit?...
asked Nov 3, 2020 in Technology by JackTerrance
0 votes
    i have migrated my iOS app parse DB to Azure, but had not finalised it yet. I have changed end ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
...