簡體   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