source

SQLSTATE[HY000]:일반 오류:테이블을 만들 수 없습니다.

manysource 2023. 8. 28. 21:14

SQLSTATE[HY000]:일반 오류:테이블을 만들 수 없습니다.

나는 센트에서 라라벨 8.21.0을 사용하고 있습니다.OS 8 서버.저는 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');
    }
}

이상한 점은 마이그레이션 테이블을 로컬 호스트 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의 외부 키 유형을 정수로 설정하였습니다.등급 테이블 마이그레이션:

$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데이터 형식을 일치시켜 제약 조건을 형성하는 테이블입니다.

언급URL : https://stackoverflow.com/questions/66704479/sqlstatehy000-general-errorcant-create-table