繁体   English   中英

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

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

我在 CentOS 8 服务器上使用 Laravel 8.21.0。 我正在使用 mariaDB。 我有 3 个表:测试、学生和成绩。 我正在尝试在成绩表上设置测试和学生的外键。 但是,当我运行迁移时,我得到错误号 150:外键约束的格式不正确。

这是我的迁移:

成绩表:

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');
    }
}

学生表迁移:

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');
    }
}

测试表:

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');
    }
}

奇怪的是,当我在 localhost WAMP 服务器上运行迁移表时,它成功创建了它,但它在 CENTOS 服务器上抛出了一个错误。 有谁知道如何解决这个问题?

我尝试过但没有奏效的事情:

・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. 

任何想法将不胜感激。 :'(

编辑:我将 student_id 的外键类型设置为 integer。 在成绩表迁移中:

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

在学生表迁移中:

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

这样做后,我收到一个新错误:

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()

改变

$table->unsignedInteger('student_id');

$table->integer('student_id');

grades表中匹配数据类型以形成约束。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM