简体   繁体   English

SQLSTATE[HY000]:一般错误:无法创建表

[英]SQLSTATE[HY000]: General error:Can't create table

I am using Laravel 8.21.0 on a CentOS 8 server.我在 CentOS 8 服务器上使用 Laravel 8.21.0。 I am using mariaDB.我正在使用 mariaDB。 I have 3 tables: tests, students and grades.我有 3 个表:测试、学生和成绩。 I am trying to set foreign key of tests and students on the grades table.我正在尝试在成绩表上设置测试和学生的外键。 However, when I run my migrations, I get errorno 150: Foreign key constraint is incorrectly formed.但是,当我运行迁移时,我得到错误号 150:外键约束的格式不正确。

Here are my migrations:这是我的迁移:

Grades table:成绩表:

class CreateGradesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('grades', function (Blueprint $table) {
            
           $table->id('id')->unique();
           $table->unsignedInteger('student_id');
           $table->string('test_id');

           $table->foreign('test_id')->references('id')->on('tests');
           $table->foreign('student_id')->references('id')->on('students');

           $table->date('testDate');
           $table->integer('testCount');
           $table->integer('vocabScore');
           $table->integer('readingScore');
           $table->integer('listeningScore');
           $table->integer('rawTotal');
           $table->integer('adjustVocabScore');
           $table->integer('adjustReadingScore');
           $table->integer('adjustlisteningScore');
           $table->integer('adjustTotal');
           $table->string('passOrfail');
           $table->timestamps();
            
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('grades');
    }
}

Student Table migrations:学生表迁移:

class CreateStudentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('students', function (Blueprint $table) {
          
            $table->integer('id')->unique();
            $table->string('name');
            $table->date('DOE');
            $table->string('belongsTo');
            $table->string('country');
            $table->string('level');
            $table->string('year');
            $table->timestamps();
            
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('students');
    }
}

Test table:测试表:

class CreateTestsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('tests', function (Blueprint $table) {
         
            $table->string('id')->unique();
            $table->string('testName');
            $table->string('level');
            $table->integer('vocabFullScore');
            $table->integer('readingFullScore');
            $table->integer('listeningFullScore');
            $table->integer('totalFullScore');
            $table->integer('vocabPassScore');
            $table->integer('readingPassScore');
            $table->integer('listeningPassScore');
            $table->integer('totalPassScore');
            $table->timestamps();
            
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('tests');
    }
}

The bizzare thing is that the migrations table gets created successfully when I run it on localhost WAMP server but it throws me an error on CENTOS server.奇怪的是,当我在 localhost WAMP 服务器上运行迁移表时,它成功创建了它,但它在 CENTOS 服务器上抛出了一个错误。 Does anyone have any idea on how to solve this?有谁知道如何解决这个问题?

Things that I have tried doing but did not work:我尝试过但没有奏效的事情:

・Changed database to InnoDB  by specifying 'engine' => 'InnoDB' on each model.
・Made sure that the order of migration is correct. First migrated student table, then tests and lastly grades.
・Ensure that the data type of the foreign key is correct on grades table. 

Any ideas would be greatly appreciated.任何想法将不胜感激。 :'( :'(

Edit: I set the foreign key type of student_id as integer.编辑:我将 student_id 的外键类型设置为 integer。 In grades table migration:在成绩表迁移中:

$table->integer('student_id');
$table->foreign('student_id')->references('id')->on('students');

In students table migration:在学生表迁移中:

$table->integer('id')->unique();

After doing this I am getting a new error:这样做后,我收到一个新错误:

Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for right syntax to use near ')' at line 1 (SQL: alter table 'grades' add constraint 'grades_test_id_foreign' foreign key ('test_id') references 'tests' ()) 

at vendor/laravel/framework/src/Illuminate/Database/Connection.php:678
catch(Exception $e){
 throw new QueryException(
  $query, $this->prepareBindings($bindings),$e
);
}
+9 vendor frames
database/migrations/2021_01_13_064711_create_grades_table.php:54
Illuminate\Support\Facades\Facade::__callStatic()

+32 vendor frames
artisan:37
Illuminate\Foundation\Console\Kernel::handle()

Change改变

$table->unsignedInteger('student_id');

to

$table->integer('student_id');

in grades table to match the datatypes to form a constraint.grades表中匹配数据类型以形成约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLSTATE [HY000]:一般错误:1005 无法创建表 - Laravel 4 - SQLSTATE[HY000]: General error: 1005 Can't create table - Laravel 4 SQLSTATE[HY000]:一般错误:1005 无法创建表 Laravel 8 - SQLSTATE[HY000]: General error: 1005 Can't create table Laravel 8 找不到表(SQLSTATE [HY000]:常规错误:1无此类表:用户) - Can't find table (SQLSTATE[HY000]: General error: 1 no such table: users) Laravel 5.4:SQLSTATE[HY000]:一般错误:1005 无法创建表“外键约束格式不正确” - Laravel 5.4: SQLSTATE[HY000]: General error: 1005 Can't create table "Foreign key constraint is incorrectly formed" SQLSTATE[HY000]: 一般错误: 1005 Can't create table `school`.`posts` (errno: 150 "Foreign key constraint is wrongly forms") - SQLSTATE[HY000]: General error: 1005 Can't create table `school`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed") 更新表格错误? (SQLSTATE [HY000]:一般错误) - Updating table error? (SQLSTATE[HY000]: General error) 为什么SQLSTATE [HY000]:一般错误? - Why SQLSTATE[HY000]: General error? SQLSTATE [HY000]:一般错误:2053 - SQLSTATE[HY000]: General error: 2053 SQLSTATE [HY000]:一般错误和array(0){} - SQLSTATE[HY000]: General error and array(0){} SQLSTATE [HY000]:一般错误 - SQLSTATE[HY000]: General error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM